Python使用open讀取txt中文內容的文件時,有可能會報錯,報錯內容如下:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc8 in position 0。
這里舉一個例子:txt=open("threekingdoms.txt","r",encoding="utf-8").read(),在運行時就會報錯。
要處理這個問題有兩個辦法,如下:
第一個辦法,將編碼方式由utf-8改為gb18030,即txt=open("threekingdoms.txt","r",encoding="gb18030").read()。
第二個辦法,將原來的txt文件另存為,在保存時將編碼由ANSI改為UTF-8。

參考:https://blog.csdn.net/xxceciline/article/details/80405129
