有没有办法按特定顺序存储PyTable列?

时间:2010-11-29 13:02:57

标签: pytables

当使用字典或类进行模式定义来调用createTable()时,似乎PyTable列按字母顺序排列。我需要建立一个特定的订单,然后使用numpy.genfromtxt()从文本中读取和存储我的数据。我的文本文件没有按字母顺序包含变量名,因为它们是PyTable。

例如,假设文本文件名为mydata.txt,并按如下方式组织:

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)

因此,希望创建一个按这些列排序的表 然后使用numpy.genfromtxt命令填充表。

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

这是期望的,因为它是简单的代码并且非常快。但是,PyTable列始终按字母顺序排序,附加数据按照所需顺序排序。我错过了一些基本的东西吗?有没有办法让表列的顺序遵循类定义顺序而不是按字母顺序排列?

1 个答案:

答案 0 :(得分:9)

是的,您可以通过几种不同的方式在表格中定义订单。最简单的方法是为每列使用pos参数。请参阅Col类的文档:

http://pytables.github.io/usersguide/libref/declarative_classes.html#the-col-class-and-its-descendants

对于您的示例,它将如下所示:

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)

希望这有帮助

相关问题