天天看點

UnicodeEncodeError: 'gbk' codec can't encode character: illegal multibyte sequence

又是python的編碼問題了,我都想哭了,windows系統預設編碼是gbk,然後你用GBK方式打開寫入的txt檔案,寫入utf-8格式的字元,就要報錯了。。。。

我的解決方案是在寫入的時候加入一個utf-8編碼:

with open(output_path, "w",encoding='utf-8') as file:
            for word in counter_list:
                file.write(word[0])
                file.write("\n")
               # print(word[0])  #輸出字典的鍵
                #print(word[1])  # 輸出字典的值      

參考文獻

UnicodeEncodeError: 'gbk' codec can't encode character: illegal multibyte sequence