我有一个包含以下数据的文本文件:
5298 10036 4 360 8
6128 11947 2 385 7
9472 18930 0 233 4
5056 9790 1 293 6
我使用以下代码读取该文件:
file1 = open("test.txt","r")
lines = file1.readlines()
BF=[map(float, line.split()) for line in lines]
这给了我以下错误:
could not convert string to float: ÿþ5
为什么我会看到这个错误?
Update:
print lines
shows:
['\xff\xfe5\x002\x009\x008\x00\t\x001\x000\x000\x003\x006\x00\t\x004\x00\t\x003\x006\x000\x00\t\x008\x00\r\x00\n', '\x006\x001\x002\x008\x00\t\x001\x001\x009\x004\x007\x00\t\x002\x00\t\x003\x008\x005\x00\t\x007\x00\r\x00\n', '\x009\x004\x007\x002\x00\t\x001\x008\x009\x003\x000\x00\t\x000\x00\t\x002\x003\x003\x00\t\x004\x00\r\x00\n', '\x005\x000\x005\x006\x00\t\x009\x007\x009\x000\x00\t\x001\x00\t\x002\x009\x003\x00\t\x006\x00\r\x00\n', '\x001\x005\x000\x006\x004\x00\t\x003\x000\x001\x006\x000\x00\t\x001\x00\t\x003\x001\x002\x00\t\x008\x00']
你有一个utf-16 http://en.wikipedia.org/wiki/Byte_order_mark#UTF-16物料清单,这是0xFE
0xFF
这被解释为ÿþ
,您需要打开文件并传递编码。
file1 = open("test.txt","r", encoding = "utf-16")
当你使用 python 2 时,你可以尝试这个:
import io
file1 = io.open("test.txt","r", encoding = "utf-16")
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)