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

发布于 2024-10-04 23:12:01 字数 936 浏览 0 评论 0原文

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

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

time(row1) bVar(row1) dVar(row1) aVar(row1) cVar(row1)

time(row2) bVar(row2) dVar(row2) aVar (行2) cVar(行2) ...

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 列始终按字母顺序排序,并且附加数据根据所需顺序排序。我在这里缺少一些基本的东西吗?有没有办法让表列的顺序遵循类定义顺序而不是按字母顺序排列?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你是暖光i 2024-10-11 23:12:01

是的,您可以通过多种不同的方式定义表中的顺序。最简单的方法是对每一列使用 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)

希望这个帮助

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文