简体   繁体   中英

Get recarray attributes/columns python

I'm trying to retrieve the column titles of a recarray, and running into considerable trouble. If I read in a .csv file using pylab's csv2rec function, I am able to access column titles in the following manner:

from pylab import csv2rec
x = csv2rec(file.csv)
x.column1
x.column2

Where 'column1' is the title of the first column, and it would return the rest of the values in the column. But, I am reading in a .csv file where I don't know what all of the values of the column titles are, and I want to be able to access them (either loop through, or set a list). This seems like it should be simple. Any ideas?

You can use x.dtype.names :

>>> import numpy as np

>>> a = np.array([0.1,0.2])
>>> b = np.array([0.3,0.4])
>>> dtype = {'names' : ['a','b'], 'formats' : ['f8', 'f8']}
>>> c = np.rec.fromarrays([a,b], dtype = dtype)
>>> c
rec.array([(0.1, 0.3), (0.2, 0.4)], 
      dtype=[('a', '<f8'), ('b', '<f8')])
>>> print c.dtype.names
('a', 'b')

Or, using your example:

[physics@aurora ~/calc ]$ cat csv.dat 
a,b
0.1,0.3
0.2,0.4

In [1]: from pylab import csv2rec

In [2]: x = csv2rec('csv.dat')

In [3]: for name in x.dtype.names:
   ...:         print name
a
b

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM