Numpy.loadtxt gives the error "not iterable"

I am trying to use numpy.loadtxtto read 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 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 that this happened because the first two columns were clearly integers, but not explicit, but I'm not even sure which object it refers to, because it will not even read only floats. How to do the job loadtxt?

Related: How to specify the format of multiple columns using dtype = ?I have problems finding out through google.

+5
source share
2 answers

usecols=(2). usecols . (2) - 2, , 2, , , : loadtxt() int. (2,) ( [2], ).

+10

, , . , , :

>>> with open('beamtest.out', 'r') as f:
...     f.readlines()
... 
['header 0\n', 'header 1\n', 'header 2\n', 'header 3\n', 'header 4\n', 
 'header 5\n', 'header 6\n', 'header 7\n', 'header 8\n', 'header 9\n', 
 'header 10\n', 'header 11\n', 'header 12\n', 'header 13\n', 
 '3 0 36373.7641026\n', '3 1 36373.7641026\n', '3 2 36373.7641026']
>>> chunk, power = numpy.loadtxt('beamtest.out', skiprows=14,
                                 usecols=(1,2), unpack=True)
>>> chunk
array([ 0.,  1.,  2.])
>>> power
array([ 36373.7641026,  36373.7641026,  36373.7641026])

, kindall answer , , usecols ; it . ((1) 1, - (1,).)

dtype :

>>> record = numpy.loadtxt('beamtest.out', skiprows=14, usecols=(1, 2), 
                           dtype={'names':('chunk', 'power'), 
                                  'formats':('i8', 'f8')}) 
>>> record
array([(0, 36373.7641026), (1, 36373.7641026), (2, 36373.7641026)], 
      dtype=[('chunk', '<i8'), ('power', '<f8')])
>>> record['chunk']
array([0, 1, 2])
>>> record['power']
array([ 36373.7641026,  36373.7641026,  36373.7641026])
+1

All Articles