1、UnicodeEncodeError: ‘gbk’ codec can’t encode byte 0xa2 in position 872:illegal multibyte sequence
发现bug定位到了with open(vocab_file, 'r') as vocab_f:
中。
解决方法: 指定编码为utf-8,如下:
with open(vocab_file, 'r', encoding='utf-8') as vocab_f:
注: 读入方式’rb’或者写入方式’wb’,默认是二进制编码格式,不需要指定编码为utf-8。
2、UnicodeEncodeError: ‘gbk’ codec can’t encode character ‘\xa0’ in position 60: illegal multibyte sequeue
发现bug定位到了print()
中,原来是print()函数自身有限制,不能完全打印所有的unicode字符。
注: 在python3中,字符就是unicode字符,字符串就是unicode字符数组。如果用以下代码测试:
print('a'=='\u0061')
会发现结果为True,足以说明两者的等价关系。
解决方法: 在python脚本最上方加入一些控制代码,如下:
import io
import sys
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf-8') #改变标准输出的默认编码