天天看点

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