UnicodeEncodeError: 'latin-1' codec can't encode characters,python3 中文亂碼

UnicodeEncodeError: 'latin-1' codec can't encode characters in position 9-13: ordinal not in range(256)spa

後來苦思冥想找資料,最後發現一個辦法,能夠解決上述問題,就是:3d

在下圖所示處加上下面箭頭所指那句,即圖後藍色代碼code

 

account = accountraw.encode("utf-8").decode("latin1")

blog

 

file="中國.xls".decode("utf-8")#將中文進行decode解碼也就是將utf-8轉爲unicode
data=xlrd.open_workbook(file)
2.控制檯輸出中文亂碼
解決方法:
print("中國").decode('utf-8').encode('gbk')
#源碼是utf-8,控制檯是默認gbk輸出,
最好本身去更改一下設置就好(在file-settings-fileEncodings-utf-8)兩個都選成utf-8,這樣就能夠直接輸出
緣由:utf-8

 

主要緣由是Excel中讀取數據亂碼解決辦法以下:unicode

相關文章
相關標籤/搜索