gbk codec can't encode character问题解决

编码问题

无论是做爬虫还是做对话系统,都免不了要进行数据处理以及从文件中读取数据。读取中文比较麻烦,会出各种编码错误。

当出现'gbk' codec can't encode character***时,解决方案如下:

1
2
3
4
5
6
import sys
import io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='gb18030')
with open(file_name,encoding='utf-8') as f:
line = f.readline()
print(line)