Python編碼問題整理(好用)
python語句:
filea=open("養老補繳明細表.csv", 'r')
print("檢測點A", filea, "監測點B")
df3=pd.read_csv(filea)
print(df3.dtypes)
輸出錯誤提示:
檢測點A<_io.TextIOWrapper name='養老補繳明細表.csv' mode='r' encoding='cp936'> 檢測點B
Exception "unhandled UnicodeDecodeError"'gbk' codec can't decode byte 0x9f in position 9: illegal multibyte sequence
解決方法:
故障點1:開啟"養老補繳明細表.csv",發現檔案內容中文字元亂碼:df1.to_csv("養老補繳明細表.csv")改為:df1.to_csv("養老補繳明細表.csv",encoding='utf_8'),檔案內容亂碼問題解決。
故障點2:'gbk' codec can't decode byte 0x9f in position 9: illegal multibyte sequence問題。修改對應語句為:
filea=open("養老補繳明細表.csv", 'r', encoding='utf-8')
問題解決。