有没有办法以特定顺序存储 PyTable 列?
当使用字典或类进行模式定义以调用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,您可以通过多种不同的方式定义表中的顺序。最简单的方法是对每一列使用 pos 参数。请参阅
Col
类的文档:http://pytables.github.io/usersguide/libref/declarative_classes.html#the-col-class-and-its-descendants
对于您的示例,它看起来像:
希望这个帮助
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 theCol
class:http://pytables.github.io/usersguide/libref/declarative_classes.html#the-col-class-and-its-descendants
For your example, it will look like:
Hope this helps