0

I'm a newbie to Python and currently I have a text file which looks like this:

# Wed 13:10:08 11-Mar-2015
 begin  aperture image1 1 1024. 139.7445
 image  image1
 aperture   1
 beam   1
 center 1024. 139.7445
 low        -1023. -4.
 high   1024. 4.
 background
         xmin -40.45428
         xmax 43.75221
         function chebyshev
         order 3
         sample -40.45428:-18.42313 20.09063:43.75221
         naverage 1
         niterate 0
         low_reject 3.
         high_reject 3.
         grow 0.
 axis    2
 curve   6
         2.
         2.
         4.
         2044.
         -0.1275881
         -0.03320996 

I want to extract '139.7445' from the sixth row ('center'). This is my code:

pos_wasp = np.loadtxt(line, skiprows=5, usecols=(3,4), unpack=True)

But when I run it, it gives an error:

IndexError: list index out of range

It should be a simple problem to solve and I've been trying to change the column numbers and the data types many times, but it still doesn't work.

3
  • 1
    np.genfromtxt(line, skip_header=5, skip_footer=21, usecols=(1,2), unpack=True) will extract array([1024., 139.7445]). Commented Jun 14, 2015 at 16:43
  • My guess is that the index error comes from the usecols value. Giving us more of the error message (lines etc) would clarify that. But more importantly this not the kind of file that loadtxt/genfromtxt is meant to handle. Commented Jun 14, 2015 at 17:11
  • That works with genfromtxt! Thank you all for your help :) Commented Jun 16, 2015 at 10:33

5 Answers 5

5

If you reached this page after getting frustrated with IndexError: list index out of range while using numpy.loadtxt, do not despair.

The reason this fails is because one or more rows in the input text file do not contain columns specified in usecols=().

To correct the error, check that all rows in the text file have the column you are trying to extract.

Sign up to request clarification or add additional context in comments.

2 Comments

I know the reason behind the issue and do not want to look at every value that contains 400K data. How can I replace missing values with 0 or some meaningful stuff and hence avoid this error.
This is it. If txt reader fails to parse csv correctly the rows wont be assembled as expected. Which leads to usecols value be greater than the number of rows for some columns.
1

I think your code is failing because loadtxt wants to read in all the lines after the one your value is on. To just get this one value, why not read the file (inpp.txt, or whatever yours is called) directly:

with open('inpp.txt') as fi:
    for line in fi:
        fields = line.split()
        if fields[0] == 'center':
            val = float(fields[2])
            break

print(val)

Comments

0

Is there a new line at the end of the text file? If so, get rid of it and see if that works.

1 Comment

I'm having the same problem, removing the carriage return character at the end of the text file did not help.
0

I think your code is failing because loadtxt wants to read in all the lines after the one your value is on as well. You can skip footer with genfromtxt as @nymk suggests, but to just get this one value, why not read the file directly:

with open('inpp.txt') as fi:
    for line in fi:
        fields = line.split()
        if fields[0] == 'center':
            val = float(fields[2])
            break

print(val)

Comments

0

After playing around, I found that for some reason I have to specify delimiter= when I use usecols=, even though numpy.loadtxt() correctly assumes the delimiter when usecols= isn't used. So try using:

delimiter=" ".

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.