5

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

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

时间(第 1 行) bVar(第 1 行) dVar(第 1 行) aVar(第 1 行) cVar(第 1 行)

时间(row2) bVar(row2) dVar(row2) aVar(row2) cVar(row2) ...

时间(行 N) bVar(行 N) dVar(行 N) aVar(行 N) cVar(行 N)

因此,希望创建一个按这些列排序的表,然后使用 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 列始终按字母顺序排列,附加数据按所需顺序排列。我在这里缺少一些基本的东西吗?有没有办法让表列的顺序遵循类定义顺序而不是按字母顺序排列?

4

1 回答 1

10

是的,您可以通过几种不同的方式在表格中定义订单。最简单的方法是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)

希望这可以帮助

于 2010-12-08T17:03:21.880 回答