想要使用 pytables 在 HDF5 数据库文件中访问/创建任意组。 该文件具有以下结构:
db
|_ user_00 # Group
|_ subjectTable # TableObject
|_ subject_00 # GroupObject
注册一个新的主题意味着在主题表中添加一行并使用主题名称创建一个组 所以我有:
def open_db(db_file, mode='r+'):
h5f = tables.openFile(db_file, mode)
return h5f
def register_new_subject(subjectName, user, db_file):
# Open db
h5f = open_db(db_file)
#Create subject
subjectGroup = h5f.createGroup(h5f.root.??????????, subjectName)
# Add subjectName to user/subjectTable
...
正如您从问号中看到的,我不知道如何继续...因为该组是特定于我卡住的用户的,新组应该是 h5f.root.[user].subjectName
有没有办法做到这一点?
更好的是有一个 pytables 方法来做到这一点吗?
为了加分,是否有一种 pythonic 的方式来做到这一点?
编辑: 这样就可以了,但是我讨厌使用 eval()。
row_str = 'h5f.root.{}'.format(user)
where = eval(row_str)
subjectGroup = h5f.createGroup(where, subjectName)
还有其他方法吗?
最佳答案
row_str = 'h5f.root.{}'.format(user)
where = eval(row_str)
subjectGroup = h5f.createGroup(where, subjectName)
关于python - 使用 pytables 访问 hdf5 文件中任意组的方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20199936/