【发布时间】:2013-06-04 20:21:45
【问题描述】:
我无法打开任何中文字符的文件,编码设置为utf-8:
text = open('file.txt', mode='r', encoding='utf-8').read()
print(text)
UnicodeEncodeError: 'charmap' codec can't encode character '\u70e6' in position 0: character maps to <undefined>
文件是 100% utf-8。
http://asdfasd.net/423/file.txt
http://asdfasd.net/423/test.py
如果我删除 encoding='utf-8' 一切正常。
这里的编码有什么问题?
我在打开文件时总是使用encoding='utf-8',我现在不知道现在发生了什么。
【问题讨论】:
-
您在读取文件时没有问题,但在写入(或打印)时遇到了问题。阅读会导致
UnicodeDecodeError,但您有一个编码问题。
标签: python encoding utf-8 python-3.x