Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python
Python
  1. วิธีแปลง Python Date String เป็น Date Object

    แปลงสตริงเป็น date object ด้วยฟังก์ชัน strptime ให้ date string และ format ที่ระบุ ตัวอย่าง import datetime date_str = 29122017 # 29 Dec 2017 format_str = %d%m%Y # format datetime_obj = datetime.datetime.strptime(date_str, format_str) print(datetime_obj.date()) ผลลัพธ์ 2017-12-29

  2. วิธีเปรียบเทียบ Python Dates

    Python date implementations รองรับ comparison operators ทั้งหมด หากใช้ datetime module สร้างและจัดการ date objects ใช้ < > <= >= ได้เลย ทำให้ compare และ validate dates ง่ายมาก ตัวอย่าง from datetime import datetime, timedelta today = datetime.today() yesterday = today - timedelta(days=

  3. วิธีแปลง Pandas Offset เป็น Python Date

    ลบ pandas offset จาก date object ได้ pandas Timestamp object แปลงเป็น string format date หรือ Date object (standard Python date) ได้ หรือใช้ timedelta จาก datetime library ตัวอย่าง from pandas.tseries.frequencies import to_offset import pandas as pd dt = pd.to_datetime(2018-01-04) - to_offset(5D) p

  4. วิธีได้วันที่สร้างและแก้ไขไฟล์ใน Python

    ดึงเวลาสร้างไฟล์: Windows ใช้ os.path.getctime(file_path) UNIX ไม่สามารถใช้ฟังก์ชันเดียวกันได้ เพราะคืนเวลาที่ attribute หรือ content เปลี่ยนล่าสุด UNIX ใช้ st_birthtime attribute ของ stat tuple ตัวอย่าง Windows: print(os.path.getctime(my_file)) 1505928271.0689342 คืนวินาทีนับจาก epoch สำหรับ UNIX

  5. Raw String Notation ใน Python Regular Expression คืออะไร

    Raw String Notation ตามเอกสาร Python raw string notation rtext ทำให้ regular expressions meaningful และไม่สับสน โดยไม่มี raw string ทุก backslash \ ใน regex จะต้อง escape ด้วย backslash อีกตัว ตัวอย่าง 2 บรรทัดนี้ทำงานเหมือนกัน: re.match(\\W(.)\\1\\W, ff ) <_sre.SRE_Match object; span=(0, 4), m

  6. วิธีใช้ re.finditer() ใน Python Regular Expression

    ตามเอกสาร Python: re.finditer(pattern, string, flags=0) คืน iterator ของ MatchObject instances ทุก non-overlapping matches สแกนซ้ายไปขวา คืนตามลำดับ รวม empty matches ตัวอย่าง import re s1 = Blue Berries pattern = Blue Berries for match in re.finditer(pattern, s1): s = match.start() e = mat

  7. วิธีใช้ Python Regex จับคู่ Date String

    กรณีแรก date string จับคู่รูปแบบ d-m-y กรณีที่สองไม่จับคู่ ตัวอย่าง import re datestring = 21-09-1991 foo = re.match((\d{2})[/.-](\d{2})[/.-](\d{4})$, datestring) print(foo.group()) datestring = 1991-09-21 foo = re.match((\d{2})[/.-](\d{2})[/.-](\d{4})$, datestring) print(foo) ผลลัพธ์ 21-09-1991 N

  8. วิธีเพิ่มประสิทธิภาพ Python Regular Expression

    Regular expressions ทำให้การกรอง pattern ใน word processing ง่ายขึ้น ดีคือ compile แล้ว reuse ได้เร็วขึ้น เนื่องจาก regex ไม่เปลี่ยนข้าม function calls จึง compile ครั้งเดียวใช้ compiled version แต่มี trade-off ต้อง balance เพื่อ optimize performance พบว่าใช้ operator in ใน regex ช่วยเร่งงาน Regex ย

  9. ทำไมต้องใช้ re.compile() ใน Python Regular Expression

    re.compile() Method re.compile(pattern, repl, string) รวม regular expression pattern เป็น pattern objects ใช้สำหรับ pattern matching ช่วยค้นหา pattern ซ้ำโดยไม่ต้องเขียนใหม่ ตัวอย่าง import re pattern = re.compile(TP) result = pattern.findall(TP Tutorialspoint TP) print(result) result2 = pattern.fin

  10. ทำไมต้องใช้ Question Mark Literal ใน Python Regular Expression

    Question mark literal ใช้หลายแบบใน Python Regular Expressions Special character ? ทำให้ RE จับคู่ 0 หรือ 1 ครั้งของ RE ก่อนหน้า ab? จับคู่ a หรือ ab Question mark ใช้ในรูปแบบต่อไปนี้ตามเอกสาร Python: ทำให้ Quantifier เป็น Non-greedy *?, +?, ?? *, +, ? qualifiers ล้วนเป็น greedy จับคู่มากที่สุดเท่าท

  11. วิธี Escape ตัวอักษรพิเศษใดๆ ใน Python Regular Expression

    Regular expressions ใช้ backslash \ บ่งบอก special forms หรืออนุญาตให้ใช้ special characters โดยไม่เรียกความหมายพิเศษ ซึ่งชนกับการใช้ backslash ใน Python string literals ตัวอย่าง จับคู่ literal backslash ต้องเขียน \\\\ เป็น pattern string เพราะ regex ต้องเป็น \\ และแต่ละ backslash ใน Python string ต

  12. วิธีใช้ Delimiter แยกสตริงใน Python Regular Expression

    re.split() Method re.split(pattern, string, [maxsplit=0]) แยกสตริงด้วย occurrences ของ pattern ที่กำหนด ตัวอย่าง import re result = re.split(ra, Dynamics) print(result) ผลลัพธ์ [Dyn, mics] del ลบ elements in-place ไม่สร้าง list ใหม่ มี argument maxsplit ค่าเริ่มต้น 0 ทำ max splits ที่ทำได้ หากกำหน

  13. วิธีดึงตัวเลขจากข้อความด้วย Python Regular Expression

    หากต้องการดึงตัวเลข/หลักตัวเลข แยกกันทีละหลัก จากข้อความ ใช้ regex: ตัวอย่าง import re s = 12345 abcdf 67 result = re.findall(r\d, s) print(result) ผลลัพธ์ [1, 2, 3, 4, 5, 6, 7] หากต้องการดึง กลุ่มตัวเลข (ต่อเนื่อง) ใช้ regex: ตัวอย่าง import re s = 12345 abcdf 67 result = re.findall(r\d+, s) prin

  14. ตัวอย่างพื้นฐานของ Python Regular Expressions

    นี่คือ 2 ตัวอย่างพื้นฐานของ Python regular expressions re.match() หาจับคู่ได้ก็ต่อเมื่อเกิดที่ต้นสตริง เรียก match() บน TP Tutorials Point TP หา TP จะจับคู่ แต่หา Tutorials จะไม่จับคู่ ตัวอย่าง import re result = re.match(rTP, TP Tutorials Point TP) print(result) ผลลัพธ์ <_sre.SRE_Match object a

Total 9001 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:37/451  20-คอมพิวเตอร์/Page Goto:1 31 32 33 34 35 36 37 38 39 40 41 42 43