参考 | 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()
就可以解决问题