我刚刚将 Anaconda 安装到 Windows 10 机器(Python 2.7.12 |Anaconda 4.2.0(64 位)|)我在从文件读取文本时遇到问题。请参阅下面的代码和输出。我想要文件中的实际文本。
谢谢!!
输出:
['\xff\xfeT\x00h\x00i\x00s\x00',
'\x00i\x00s\x00',
'\x00a\x00',
'\x00t\x00e\x00s\x00t\x00.\x00',
'\x00',
'\x00',
'\x00',
'\x00T\x00h\x00i\x00s\x00',
'\x00i\x00s\x00',
'\x00a\x00',
'\x00t\x00e\x00s\x00t\x00']
代码:
try:
with open('test.txt', 'r') as f:
text = f.read()
except Exception as e:
print e
print text.split()
测试.txt:
This is a test.
This is a test
我最幸运的是使用该io
模块以显式编码打开文件。
import io
with io.open(FILE, 'r', encoding='utf-16') as f:
job = f.read()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句