numpy.loadtxt gives "not iterable" error

input, numpy, python

Solution

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

Problem

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.

Original source