Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy.loadtxt gives "not iterable" error

I'm trying to use numpy.loadtxt to read the data in a file that looks like this:

## 14 line of header
3 0 36373.7641026
3 1 36373.7641026
3 2 36373.7641026
...

And when I give it this:

>>> chunk, power = numpy.loadtxt(bf,skiprows=14,usecols=(1,2),unpack=True)

Or even this:

>>> power = numpy.loadtxt(bf,skiprows=14,usecols=(2))

It says, TypeError: 'int' object is not iterable

I assumed it was because the first two columns were clearly integers not float, but now I'm not even sure which int object it's referring, because it won't even read just the floats. How do I make loadtxt work?

Related: How do I specify the format of multiple columns using dtype = ? I'm having trouble figuring it out via google.

like image 957
Loon Unit Avatar asked May 25 '12 17:05

Loon Unit


1 Answers

In your second example, the problem is likely usecols=(2). usecols must be a sequence. (2) is the integer 2, not a one-element tuple containing 2, and is likely what the error message is complaining about: loadtxt() is trying to iterate over an int. Use (2,) (or [2] if you prefer).

like image 96
kindall Avatar answered Sep 30 '22 13:09

kindall