UnicodeEncodeError: 'charmap'编解码器无法编码字符,该字符映射为<undefined>。

5

我在使用Python 2.7.3时遇到了写入Unicode格式文件的问题,程序报错如下:

UnicodeEncodeError: 'charmap' codec can't encode character u'\u2019' in position 1006: character maps to <undefined>

这是我的代码示例:错误出现在第f3.write(text)行。
f = codecs.open("PopupMessages.strings", encoding='utf-16')
text = f.read()
print text
f.close()

f3 = codecs.open("3.txt", encoding='utf-16', mode='w')
f3.write(text)
f3.close()

我尝试使用'utf-8'和'utf-8-sig',但并没有帮助我。 我的源文件中有这样的符号:['\",;?*&$#@%]和不同语言的符号。 我该如何解决这个问题? 请帮帮我,我首先在stackoverflow上阅读了信息,但没有帮助我。

你使用的平台是什么(Windows、Mac OS、Linux等)? - Roman Susi
我正在使用Windows 7 x64。 - yozhik
4
我认为错误并不在f3.write(text),而在于print text。参考链接:http://wiki.python.org/moin/PrintFails。 - bobince
谢谢,我刚刚也定位了这个问题。你说得对,这是打印文本的问题。 - yozhik
如果你愿意的话,你可以回答我的问题,我会接受它。这很有用。 - yozhik
1个回答

0
返回翻译后的文本:

删除此行:

print text

它应该能够工作


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接