Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python
Python
  1. Modifier 'b' ทำอะไรเมื่อเปิดไฟล์ด้วย Python?

    Modifier b เปิดไฟล์ที่ระบุใน Binary Mode ไฟล์ Binary คือไฟล์ที่ Format ไม่ได้ประกอบด้วยอักขระที่อ่านได้ Binary Files อาจเป็น Image Files เช่น JPEG หรือ GIF, Audio Files เช่น MP3 หรือ Binary Document Formats เช่น Word หรือ PDF ใน Python ไฟล์ถูกเปิดใน Text Mode โดย Default เพื่อเปิดไฟล์ใน Binary Mode

  2. Modifier 'U' ทำอะไรเมื่อเปิดไฟล์ด้วย Python?

    ใน Python ที่มี Universal Newline Support พารามิเตอร์ Mode ของ open() สามารถเป็น U ได้ หมายถึง Open for input as a text file with universal newline interpretation สิ่งนี้จำเป็นสำหรับ Cross-Platform Support เนื่องจาก Newlines บน Unix OS แทนด้วย Single Character \n ในขณะที่บน Windows แทนด้วย 2 Charact

  3. วิธีระบุ Buffer Size เมื่อเปิดไฟล์ใน Python

    หากคุณดู Function Definition ของ open - open(name[, mode[, buffering]]) จะเห็นว่าใน Python 2 มันรับ 3 Arguments ตัวที่สามคือ Buffering Argument Buffering นี้เป็น Optional ระบุขนาด Buffer ที่ต้องการของไฟล์: 0 หมายถึง Unbuffered, 1 หมายถึง Line Buffered, ค่าบวกอื่นๆ หมายถึงใช้ Buffer ขนาดประมาณนั้น (ใ

  4. วิธีพิมพ์ Values ทั้งหมดของ Dictionary ใน Python

    Dictionary Object มีเมธอด values() ที่ทำงานนี้ให้ >>> D1 = {1: a, 2: b, 3: c} >>> D1.values() dict_values([a, b, c]) >>> list(D1.values()) [a, b, c] คุณยังสามารถได้ Value ที่ตรงกันโดย Iterate ผ่าน List ของ Keys ที่คืนค่าโดยเมธอด keys() ของ Dictionary >>> L1 = l

  5. วิธี Iterate ผ่าน Dictionary ใน Python

    มี 2 วิธีในการ Iterate ผ่าน Python Dictionary Object วิธีหนึ่งคือดึง Value ที่เกี่ยวข้องสำหรับแต่ละ Key ใน List ของ keys() >>> D1 = {1: a, 2: b, 3: c} >>> for k in D1.keys(): ... print(k, D1[k]) 1 a 2 b 3 c มีเมธอด items() ของ Dictionary Object ซึ่งคืนค่า List of Tuples แต่ละ

  6. วิธีจัดเรียง Dictionary ใน Python ตาม Keys

    Standard Distribution ของ Python มีโมดูล collections ซึ่งมีนิยามของ High-Performance Container Data Types OrderedDict เป็น Subclass ของ Dictionary ที่จำลำดับของ Entries ที่เพิ่มใน Dictionary Object เมื่อ Iterate Over OrderedDict Items จะถูกคืนค่าในลำดับที่ Keys ถูกเพิ่มครั้งแรก >>> from co

  7. วิธีจัดเรียง Dictionary ใน Python ตาม Values

    Standard Distribution ของ Python มีโมดูล collections ซึ่งมีนิยามของ High-Performance Container Data Types OrderedDict เป็น Subclass ของ Dictionary ที่จำลำดับของ Entries ที่เพิ่มใน Dictionary Object เมื่อ Iterate Over OrderedDict Items จะถูกคืนค่าในลำดับที่ Keys ถูกเพิ่มครั้งแรก >>> from co

  8. ขีดจำกัดความยาวสูงสุดของ String ใน Python (sys.maxsize)

    เหมือนกับ List ความยาวสูงสุดของ String ขึ้นอยู่กับ Platform โดยอ้างอิง sys.maxsize ตัวอย่าง sys.maxsize 9223372036854775807 # 2⁶³ - 1 บน 64-bit รายละเอียด sys.maxsize = ค่าสูงสุดของ Py_ssize_t (C Type สำหรับขนาด/Index) ใช้ร่วมกับ: List, String, Dict, Tuple, Bytes, Bytearray, และ Containers อื่น

  9. ขีดจำกัดขนาดสูงสุดของ List ใน Python (sys.maxsize)

    ขนาดสูงสุดของ List ขึ้นอยู่กับ Platform (Address Space และ/หรือ RAM) ค่าคงที่ sys.maxsize แสดงขีดจำกัดทฤษฎี ตัวอย่าง sys.maxsize 9223372036854775807 # 2⁶³ - 1 บน 64-bit System ความหมายของ sys.maxsize ค่าบวกสูงสุดของ Py_ssize_t (Signed Size Type ของ Python C API) เป็นขีดจำกัดสูงสุดของ: List Leng

  10. การอ่านไฟล์ทั้งหมดเข้า Buffer ใน Python (f.read())

    เมธอด read() โดยไม่ระบุ size จะอ่าน ไฟล์ทั้งหมด เข้า String/Bytes และคืนค่า (จนถึง EOF) ตัวอย่าง with open(my_file.txt, r) as f: ... file_content = f.read() # อ่านทั้งไฟล์ ... print(file_content) โหมด Binary vs Text with open(file.bin, rb) as f: ... content = f.read() # bytes ข้อค

  11. การอ่านไฟล์ด้วย Buffer Size จำกัดใน Python (f.read(size))

    เมธอด read(size) ของ File Object อ่านข้อมูล สูงสุด size ไบต์ จากตำแหน่ง Pointer ปัจจุบัน ตัวอย่าง: อ่าน 10 ไบต์ with open(my_file.txt, r) as f: ... print(f.read(10)) # อ่าน 10 ไบต์แรก Hello worl สมมติเนื้อหาไฟล์คือ Hello world! → อ่าน 10 ไบต์ ได้ Hello worl การอ่านแบบทีละชิ้น (Chunked Readi

  12. การลบ Directory Tree ทั้งหมด (Non-empty) ใน Python (shutil.rmtree)

    ลบโฟลเดอร์ที่มีไฟล์/ซับโฟลเดอร์อยู่ภายใน (Non-empty Directory) ต้องใช้ shutil.rmtree() ตัวอย่าง shutil.rmtree(my_folder) # ลบ my_folder และทุกอย่างภายใน ลบ Empty Directory (โฟลเดอร์ว่าง) os.rmdir(empty_folder) # ลบได้เฉพาะโฟลเดอร์ว่าง ความแตกต่าง shutil.rmtree vs os.rmdir Functionลบ Non-emp

  13. การสร้าง Tar File ใน Python (tarfile)

    ใช้โมดูล tarfile สร้าง Tar Archive (รองรับ Compression: gz, bz2, xz) จาก Directory โค้ดสร้าง Tar (Complete) import os import tarfile def tardir(path, tar_name): with tarfile.open(tar_name, w:gz) as tar_handle: for root, dirs, files in os.walk(path): for file in files:

  14. การสร้าง Zip File ใน Python (zipfile)

    ใช้โมดูล zipfile สร้าง Zip Archive จาก Directory โดยเดิน Tree ด้วย os.walk โค้ดสร้าง Zip (Complete) import os import zipfile def zipdir(path, ziph): for root, dirs, files in os.walk(path): for file in files: filepath = os.path.join(root, file) arcname = os.path.

  15. การเพิ่มไฟล์ลง Tar File ใน Python (tarfile + os.walk)

    ใช้ tarfile สร้าง Tar Archive (รองรับ Compression) เดิน Directory Tree ด้วย os.walk ฟังก์ชัน Tar Directory import os import tarfile def tardir(path, tar_name): with tarfile.open(tar_name, w:gz) as tar_handle: # w:gz = write gzip compressed for root, dirs, files in os.walk(path):

  16. การเพิ่มไฟล์ลง Zip File ใน Python (zipfile + os.walk)

    ใช้ zipfile สร้าง Zip Archive ของ Directory เดิน Directory Tree ด้วย os.walk เพิ่มไฟล์ทีละไฟล์แบบ Recursive ฟังก์ชัน Zip Directory import os import zipfile def zipdir(path, ziph): Add all files in path to zip handle ziph for root, dirs, files in os.walk(path): for file in files:

  17. การแยกไฟล์จาก Tar File ใน Python (tarfile)

    ใช้โมดูล tarfile อ่าน/เขียน Tar Files (รองรับ .tar, .tar.gz, .tar.bz2, .tar.xz) Extract ทั้งหมด (extractall) import tarfile with tarfile.open(my_tar.tar.gz) as my_tar: my_tar.extractall(./my_folder) # ระบุโฟลเดอร์ปลายทาง # my_tar.close() # Auto-close ด้วย with Extract ไฟล์เดี่ยว (extract

Total 9001 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:29/451  20-คอมพิวเตอร์/Page Goto:1 23 24 25 26 27 28 29 30 31 32 33 34 35