这里是我的代码:Python读取txt文件作为输入:IndexError:列表索引超出范围?
if __name__ == '__main__':
fid = open ('200502.txt')
data = fid.readlines()
data = map (string.strip, data)
x = []
y = []
da = []
for d in data:
s = d.split()
x.append(float(s[0])/10000.0)
y.append(float(s[1])/10000.0)
da.append(float(s[2]))
当我运行它,我得到了:
Traceback (most recent call last):
File "plot_data.py", line 286, in ?
x.append(float(s[0])/10000.0)
IndexError: list index out of range
的200502.txt(840KB)文件是这样的:
1131087 224529 3.923
1131096 224529 3.958
1131106 224530 3.897
1131116 224530 3.917
1131126 224530 3.847
(....)
如果我们正在考虑风格问题,将每行的x,y和数据值加载到单独的列表中是有点可疑的决定。对象,列表,元组或列表的列表似乎更明智。 – 2012-08-02 03:31:05
@ sr2222:很容易修复:'x,y,da = np.loadtxt('200502.txt',unpack = True); x * = 1e-5; y * = 1e-5' – jfs 2012-08-02 03:48:08