我正在尝试使用Python3.2读取一些文件,其中有些文件可能包含Unicode编码,而其他文件则不包含。
当我尝试执行以下代码时:
file = open(item_path + item, encoding="utf-8")
for line in file:
print (repr(line))
我遇到了一个错误:
UnicodeEncodeError: 'ascii' codec can't encode characters in position 13-16: ordinal not in range(128)
我正在遵循这里的文档:http://docs.python.org/release/3.0.1/howto/unicode.html。
为什么Python在代码的任何时候都要尝试将其编码为ascii?