[SciPy-user] Sort by first n columns in an array

Neil Crighton neilcrighton at gmail.com
Fri Jul 10 07:26:20 EDT 2009


Dharhas Pothina <Dharhas.Pothina <at> twdb.state.tx.us> writes:

> >>> Neil Crighton <neilcrighton <at> gmail.com> 7/8/2009 6:14 PM >>>
> >Something like this should work:
> >
> >data = np.genfromtxt(filename, dtype=None, usecols=(0,1,2),
> >                    names='year,month,day')
> >data.sort(order=['year', 'month', 'day'])
> 
> That works if I know the number of columns prior to reading the file. In this
case I'm reading a file with an
> unknown number of columns. I know that the first six columns are the date 
fields.
> 

If you know the first six columns are the date fields, can't you use

data = np.genfromtxt(filename, dtype=None, usecols=(0,1,2,3,4,5),
                     names='year,month,day,hour,min,sec')

?

That doesn't make any assumption about the number of columns (as long as 
there's at least six of them).


Neil






More information about the SciPy-User mailing list