Importing a text file gives error

好久不见. 提交于 2019-12-06 09:14:23

You have a utf-16 BOM, this is 0xFE 0xFF which is interpreted as ÿþ, you need to open the file and pass the encoding.

file1 = open("test.txt","r", encoding = "utf-16")

As you using python 2 you could try this:

import io
file1 = io.open("test.txt","r", encoding = "utf-16")
import io
file1 = io.open("test.txt","r",encoding='utf-16')
lines = file1.readlines()
BF=[map(float, line.split()) for line in lines]
print BF

Result:

[[5298.0, 10036.0, 4.0, 360.0, 8.0], [6128.0, 11947.0, 2.0, 385.0, 7.0], [9472.0, 18930.0, 0.0, 233.0, 4.0], [5056.0, 9790.0, 1.0, 293.0, 6.0]]

There could be a possibility that there is a line break included at the end if each line, why dont you print line.split() for each line in lines; just to confirm if the numbers split correctly or not....

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!