[python]讀取編碼為UTF-8的檔案

  • 1451
  • 0

encoding='UTF-8'

fileName="text.txt"

file1=open(fileName,"r",encoding='UTF-8')
#然後就可以開始用迴圈把資料取出來了

#若是取出來的資料開頭有"\ufeff"等字串,表示檔案可能是utf-8-sig,關鍵字:utf-8 with BOM
#此時改採用以下寫法即可:
file2=open(fileName,"r",encoding='UTF-8-sig')