Python getline()问题

Python getline()问题,python,file,getline,Python,File,Getline,我试图从文件中获取数据并将其存储在vector中,但我发现了一些困难。 这就是我的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])) In

我试图从文件中获取数据并将其存储在vector中,但我发现了一些困难。 这就是我的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版本。

我认为您的错误是您错过了使用您应该做的:

from numpy import array, append
from linecache import getline
print 'read file'
t = []
b = getline('file.data',4).split()
t.append(int(b[0]))

getline
的第一个参数是文件名

b = getline('file.data',4).split()