Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python
Python
  1. วิธีอ่าน Text File เข้า List/Array ใน Python

    f = open(my_file.txt, r+) my_file_data = f.read() f.close() อ่านทั้งไฟล์ทีเดียว ใช้ list comprehension อ่านทีละบรรทัดเก็บใน list: f = open(my_file, r+) lines = [line for line in f.readlines()] f.close()

  2. วิธีเปิดไฟล์ใน Binary Mode ด้วย Python

    Binary files = ไฟล์ที่ไม่ใช่ readable characters (JPEG, GIF, MP3, Word, PDF) Python เปิด text mode default เพิ่ม b ใน mode สำหรับ binary: ตัวอย่าง f = open(my_file.mp3, rb) file_content = f.read() f.close() เปิด binary read mode เก็บเนื้อหาใน file_content

  3. วิธีเปิด Binary File ใน Append Mode ด้วย Python

    Binary files = ไฟล์ที่ไม่ใช่ readable characters (รูป GIF, เสียง MP3, Word/PDF) เพิ่ม b ใน mode สำหรับ binary append mode ใช้ ab: ตัวอย่าง f = open(my_file.mp3, ab) file_content = f.read() f.close() เปิด binary append mode เก็บเนื้อหาใน file_content

  4. วิธีเปิด Binary File ใน Read/Write Mode ด้วย Python

    ระบุ mode w+b สำหรับ binary read/write (w=write, b=binary): f = open(my_file.mp3, w+b) file_content = f.read() f.write(bHello) f.close() จะเปิด binary read/write เก็บเนื้อหาใน file_content และเขียน Hello แบบ binary ทับไฟล์ สามารถใช้ r+b แทน (ไม่ truncate ไฟล์)

  5. ความแตกต่างหลักระหว่าง Python 2.7.x และ Python 3.x

    Python 3.0 ปล่อย Dec 2008 แก้ไขข้อบกพร่องรุ่นเก่า หลักการ: ลด feature duplication โดยลบวิธีเก่า ไม่ backward compatible โค้ด 2.x รันบน 3.x ไม่ได้ 2.7 เป็น major release สุดท้ายของ 2.x ความแตกต่างเด่น: print - keyword ใน 2.7, built-in function ใน 3.x (ต้องมีวงเล็บ) raw_input() - deprecated ใน 3.x ใช

  6. วิธีเขียน Comments ใน Python

    Comment คือข้อความอธิบายโค้ด ถูก interpreter ข้าม เริ่มด้วย # จบสิ้นบรรทัด # this is a comment print(Hello World) print(Welcome to TutorialsPoint) # this is also a comment after statement Python ไม่มี multi-line comment block ต้องวาง # ทุกบรรทัด IDE หลายตัวมี shortcut comment block Triple-quoted st

  7. วิธีสร้าง Multi-line Comments ใน Python

    Comment ใน Python เริ่มด้วย # จบที่สิ้นบรรทัด ไม่มี block comment แบบ C (/* ... */) คอมเมนต์หลายบรรทัด ให้วาง # นำหน้าทุกบรรทัด: # comment1 # comment2 # comment3 print(Hello World) Triple-quoted string ก็ถือเป็น comment ได้ ถ้าไม่ใช่ docstring: comment1 comment2 comment3 print(Hello World)

  8. วิธีเปิดไฟล์เพื่อเขียนใน Python

    ระบุ mode w สำหรับ write (ทับไฟล์เดิม): f = open(my_file.txt, w) f.write(Hello World) f.close() ปลอดภัยกว่า ใช้ with (โค้ดต้นฉบับมี typo mode r ต้องเป็น w): with open(my_file.txt, w) as f: f.write(Hello World)

  9. Attributes ของ File Object ใน Python

    File object มี methods และ attributes มาก รายการเต็ม: Python docs Methods สำคัญ: close() - ปิดไฟล์ next() - iterator ดึงบรรทัดถัดไป read([size]) - อ่านสูงสุด size bytes readline([size]) - อ่าน 1 บรรทัด seek(offset[, whence]) - ตั้งตำแหน่ง tell() - ดูตำแหน่งปัจจุบัน write(str) - เขียน string writeli

  10. วิธี Rename ไฟล์ใน Python

    ใช้ os.rename เปลี่ยนชื่อไฟล์: >>> import os >>> os.rename(a.txt, b.txt) หรือ shutil.move: ตัวอย่าง >>> import shutil >>> shutil.move(a.txt, b.txt) ถ้าไฟล์อยู่คนละโฟลเดอร์ ให้ระบุ absolute path ทั้งคู่

Total 9001 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:21/451  20-คอมพิวเตอร์/Page Goto:1 15 16 17 18 19 20 21 22 23 24 25 26 27