问题
It seems that the PyTable columns are alphabetically ordered when using both dictionary or class for schema definition for the call to createTable(). My need is to establish a specific order and then use numpy.genfromtxt() to read and store my data from text. My text file does not have the variable names included alphabetically as they are for the PyTable.
For example, assuming text file is named mydata.txt and is organized as follows:
time(row1) bVar(row1) dVar(row1) aVar(row1) cVar(row1)
time(row2) bVar(row2) dVar(row2) aVar(row2) cVar(row2) ...
time(rowN) bVar(rowN) dVar(rowN) aVar(rowN) cVar(rowN)
So, the desire is to create a table that is ordered with these columns and then use the numpy.genfromtxt command to populate the table.
# Column and Table definition with desired order
class parmDev(tables.IsDescription):
time = tables.Float64Col()
bVar = tables.Float64Col()
dVar = tables.Float64Col()
aVar = tables.Float64Col()
cVar = tables.Float64Col()
#...
mytab = tables.createTable( group, tabName, paramDev )
data = numpy.genfromtxt(mydata.txt)
mytab.append(data)
This is desired because it is straightforward code and is very fast. But, the PyTable columns are always ordered alphabetically and the appended data is ordered according to the desired order. Am I missing something basic here? Is there a way to have the order of the table columns follow the class definition order instead of being alphabetical?
回答1:
Yes, you can define an order in tables in several different ways. The easiest one is to use the pos
parameter for each column. See the docs for the Col
class:
http://pytables.github.io/usersguide/libref/declarative_classes.html#the-col-class-and-its-descendants
For your example, it will look like:
class parmDev(tables.IsDescription):
time = tables.Float64Col(pos=0)
bVar = tables.Float64Col(pos=1)
dVar = tables.Float64Col(pos=2)
aVar = tables.Float64Col(pos=3)
cVar = tables.Float64Col(pos=4)
Hope this helps
来源:https://stackoverflow.com/questions/4303986/is-there-a-way-to-store-pytable-columns-in-a-specific-order