1

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 
(....)

2 Answers 2

3

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]
Sign up to request clarification or add additional context in comments.

2 Comments

If we are getting in to questions of style, loading the x, y, and data values from each line in to separate lists is somewhat of a questionable decision to begin with. A list of objects, lists, tuples, or dicts would seem to be more sensible.
@sr2222: It is easy to fix: x,y,da = np.loadtxt('200502.txt', unpack=True); x *= 1e-5; y *= 1e-5
0

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].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.