无法使用自然命名检索PyTables中的数据集 [英] Cannot retrieve Datasets in PyTables using natural naming

查看:75
本文介绍了无法使用自然命名检索PyTables中的数据集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是PyTables的新手,我想使用自然命名从HDF5检索数据集,但使用以下输入却遇到此错误:



f = table.open_file( filename.h5, r)



f.root.group-1.dataset-1.read()



group / 没有一个名为 group

$ b的孩子
$ b

,如果我尝试:



f.root.group\-1.dataset\-1.read()



group / 没有一个名为 group

换行符后出现意外字符



我无法更改组名,因为来自实验的大数据。

解决方案

您不能在自然命名中使用减号(连字符),因为它不是有效的字符作为Python变量名( group-1 dataset-1 看起来像是减法运算!)请参见以下讨论:

为什么-python-does-not-allow-hyphens



如果您有使用此命名约定的组和数据集,则必须使用 file.get_node() 方法来访问它们。这是一个简单的代码片段来演示。第一部分创建2个组和表(数据集)。 #1在组和表名称中使用 _ ,而#2在组和表名称中使用-。第二部分使用自然命名访问数据集#1,并使用 file.get_node()

$ p $ b $访问数据集#2。 b $ b

 导入表为tb 
导入numpy为np

#创建具有2个组和数据集的h5文件:
# '/ group_1','ds_1':支持自然命名
#'/ group-2','ds-2':不支持自然命名
h5f = tb.open_file('SO_55211646.h5', 'w')

h5f.create_group('/','group_1')
h5f.create_group('/','group-2')

mydtype = np.dtype([[('a',float),('b',float),('c',float)])
h5f.create_table('/ group_1','ds_1',说明= mydtype)
h5f.create_table('/ group-2','ds-2',description = mydtype)

#关闭,然后重新打开文件
h5f。 close()

h5f = tb.open_file('SO_55211646.h5','r')

testds_1 = h5f.root.group_1.ds_1.read()
print(testds_1.dtype)

#以下是无效的Python语句:
#testds-2 = h5f.root.group-2.ds-2.read()
#print(testds-2 .dtype)

testds_2 = h5f.get_node('/ group-2','ds-2')。read()
打印(testds_2.dtype)

h5f.close()


I'm new in PyTables and I want to retrieve a dataset from a HDF5 using natural naming but I'm getting this error using this input:

f = tables.open_file("filename.h5", "r")

f.root.group-1.dataset-1.read()

group / does not have a child named group

and if I try:

f.root.group\-1.dataset\-1.read()

group / does not have a child named group

unexpected character after line continuation character

I can't change names in the groups because is big data from an experiment.

解决方案

You can't use the minus (hyphen) sign with Natural Naming because it's not a valid character as a Python variable name (group-1 and dataset-1 look like a subtraction operation!) See this discussion:
why-python-does-not-allow-hyphens

If you have groups and datasets that use this naming convention, you will have to use the file.get_node() method to access them. Here's a simple code snippet to demonstrate. The first part creates 2 groups and tables (datasets). #1 uses _ and #2 uses - in the group and table names. The second part accesses dataset #1 with Natural Naming, and dataset #2 with file.get_node()

import tables as tb
import numpy as np

# Create h5 file with 2 groups and datasets:
# '/group_1', 'ds_1' : Natural Naming Supported
# '/group-2', 'ds-2' : Natural Naming NOT Supported
h5f = tb.open_file('SO_55211646.h5', 'w')

h5f.create_group('/', 'group_1')
h5f.create_group('/', 'group-2')

mydtype = np.dtype([('a',float),('b',float),('c',float)])
h5f.create_table('/group_1', 'ds_1', description=mydtype )
h5f.create_table('/group-2', 'ds-2', description=mydtype )

# Close, then Reopen file READ ONLY
h5f.close()

h5f = tb.open_file('SO_55211646.h5', 'r')

testds_1 = h5f.root.group_1.ds_1.read()
print (testds_1.dtype)

# these aren't valid Python statements:
#testds-2 = h5f.root.group-2.ds-2.read()
#print (testds-2.dtype)

testds_2 = h5f.get_node('/group-2','ds-2').read()
print (testds_2.dtype)

h5f.close()

这篇关于无法使用自然命名检索PyTables中的数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆