Python3 讀寫utf-8文本文件

python默認用系統字符集讀取文件,例如windows下就會用GBK方式解碼,若是要讀取utf-8編碼文本文件,須要指定文件使用的字符集python

with open('testRead.txt', 'r', encoding='utf-8') as f:
    for each_line in f:
        Pass
with open('testWrite.txt', 'w', encoding='utf-8') as f:
    f.write('寫入的內容'+ '\n')  # 若是寫入後要換行就要加\n
相關文章
相關標籤/搜索