Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีอ่านและเขียน Unicode (UTF-8) Files ใน Python

โมดูล io แนะนำให้ใช้ ตรงกับ Python 3's Open Syntax:

ตัวอย่าง

import io

# อ่าน UTF-8
with io.open(filename, 'r', encoding='utf8') as f:
    text = f.read()
    # process Unicode text

# เขียน UTF-8
with io.open(filename, 'w', encoding='utf8') as f:
    f.write(text)

ใช้ with Statement ปิดไฟล์อัตโนมัติ รองรับ Unicode เต็มรูปแบบ