Cannot retrieve Datasets in PyTables using natural naming

半腔热情 提交于 2019-12-24 11:31:57

问题


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.


回答1:


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()


来源:https://stackoverflow.com/questions/55211646/cannot-retrieve-datasets-in-pytables-using-natural-naming

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!