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

tnt*_*tnt 4 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)
Run Code Online (Sandbox Code Playgroud)

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

Fra*_*ted 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)
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助