代码如下:
s = 'Waitematā'
w = open('test.txt','w')
w.write(s)
w.close()
我得到以下错误
UnicodeEncodeError: 'charmap' codec can't encode character '\u0101' in position 8: character maps to <undefined>
字符串将使用宏a,ā打印。但是,我无法将其写入.txt或.csv文件
我能把我们的马克龙a,ā换成没有马克龙吗?提前谢谢你的帮助
Tags:
请注意,如果使用
open('text.txt', 'w')
打开文件并向其中写入字符串,则不是向文件写入字符串,而是将编码字符串写入文件。所使用的编码取决于LANG
环境变量或其他因素要强制使用UTF-8,正如您在标题中所建议的,您可以尝试以下方法:
如open所述:
并非所有编码都支持所有Unicode字符。由于编码在未指定时依赖于平台,因此在读取或写入文本文件时显式地调用编码更好、更便于移植。UTF-8支持所有Unicode代码点:
相关问题 更多 >
编程相关推荐