Python3报错:UnicodeEncodeError: 'gbk' codec can't encode character '\U0001f00f' in xxxxx

查了好多文章资料,所试验方法如下:

1.sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding=‘gb18030’) #改变标准输出的默认编码 (无效,Python3下报错说没有属性buffer
2. .encode(‘GB18030’) /.encode(“GBK”,“ignore”) (也改了还是没有用!)
3. .decode(“UTF-8”,‘ignore’) /.decode(“UTF-8”,‘replace’) (已试,无效!)

最后查到有效的方法:只需在open()函数中加入encoding=‘utf8’,即可!

 open(fileName, 'a',encoding='utf8') as fp:

你可能感兴趣的:(Python3报错:UnicodeEncodeError: 'gbk' codec can't encode character '\U0001f00f' in xxxxx)