UnicodeEncodeError: 'gbk' codec can't encode character '\xc9' in position 3: illegal multibyte sequence

在将列表写入csv文件时,出现以下错误:

经过百度之后,得知错误原因是python解释器会用gbk编码去解析我们的网络数据流txt,然而txt此时已经是decode过的unicode编码。因此我在写入文件的命令中添加编码方式

file = open('F:/univlist.csv', 'w',newline='',encoding='utf-8')
运行成功,但文件中出现乱码


又经过尝试,才输出正常
file = open('F:/univlist.csv', 'w',newline='',encoding='gb18030')

猜你喜欢

转载自www.cnblogs.com/123-B/p/10877591.html