参考 | PythonBug: UnicodeEncodeError: ‘gbk‘ codec can‘t encode character

参考 | PythonBug: UnicodeEncodeError: ‘gbk’ codec can’t encode character

将str字符流写入 txt 文件内容时, 报这个错, 多数都是编码问题
只需要将报错代码的第一行:

with open('./S02E02_cn.txt', 'w') as wf:
	wf.write(cn)
wf.close()

open 的括号里面, 加上 encoding 参数:

with open('./S02E02_cn.txt', 'w', encoding='utf-8') as wf:
	wf.write(cn)
wf.close()

就可以解决问题

猜你喜欢

转载自blog.csdn.net/JackyAce6880/article/details/125701919