問題描述
Python 讀取 txt 文件作為輸入:IndexError: list index out of range? (Python read txt file as input: IndexError: list index out of range?)
Here is my code:
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]))
When I run it I got:
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
The 200502.txt (840kb) file is like:
1131087 224529 3.923
1131096 224529 3.958
1131106 224530 3.897
1131116 224530 3.917
1131126 224530 3.847
(....)
‑‑‑‑‑
參考解法
方法 1:
This is a fragile way to load your data. My guess is it's breaking on a newline at the end of the file, or something like that. Regardless, you should load your file with numpy.loadtxt
instead (or csv
module if you don't have access to numpy
).
To get you started on that:
>>> import numpy
>>> data = numpy.loadtxt('/tmp/200502.txt')
>>> xs = data.T[0]/10000.
>>> ys = data.T[1]/10000.
>>> da = data.T[2]
方法 2:
There is at least one element in your input file that contains no data. You are iterating over data
, and one of the elements is producing a 0‑length list from d.split()
. I'd also suggest considering loading your data in to a more structured organization. Something like loaded_data = [zip(('x', 'y', 'da'), d.split()) for d in data]
.