我试图从文件中获取数据并将其存储在矢量中,但我发现了一些困难。这就是我的Python脚本的样子:
from numpy import array, append
from linecache import getline
print 'read file'
t = []
f = open('file.dat', 'r')
b = getline('f',4).split()
t.append(int(b[0]))在运行之后,我获得了以下信息:
t.append(int(b[0]))
IndexError: list index out of range当我检查b显示为空时:
>>b
[]在file.dat的第4行,我的数字是4,它只是该行中的一个条目。有没有人怎么知道出了什么问题?我使用的是2.7版本的Python。
发布于 2012-07-13 04:52:57
我认为您的错误是您错过了使用linecache.getline,您应该这样做:
from numpy import array, append
from linecache import getline
print 'read file'
t = []
b = getline('file.data',4).split()
t.append(int(b[0]))发布于 2012-07-13 04:53:42
getline的第一个参数是文件名。
b = getline('file.data',4).split()https://stackoverflow.com/questions/11460427
复制相似问题