如何从HDF5文件中提取数据以填充Pytables表?
我试图在Python写一个Discord Bot。该机器人的目标是填充一个带有用户的条目的表格,这些桌子被检索到用户名,gamename和gamepswd。然后,让特定用户提取这些数据并删除已解决的条目。我在Google上找到了第一个工具来管理桌子,因此可以在HDF5文件中填充表格,但是我无法检索它们。
重要的是要说我以前从未在Python中编码。
这就是我声明对象并创建一个存储条目的文件的方式。
class DCParties (tables.IsDescription):
user_name=StringCol(32)
game_name=StringCol(16)
game_pswd=StringCol(16)
h5file = open_file("DCloneTable.h5", mode="w", title="DClone Table")
group = h5file.create_group("/", 'DCloneEntries', "Entries for DClone runs")
table = h5file.create_table(group, 'Entries', DCParties, "Entrées")
h5file.close()
这就是我填写
h5file = open_file("DCloneTable.h5", mode="a")
table = h5file.root.DCloneEntries.Entries
particle = table.row
particle['user_name'] = member.author
particle['game_name'] = game_name
particle['game_pswd'] = game_pswd
particle.append()
table.flush()
h5file.close()
所有这些工作的方式,我可以看到我的条目用HDF5查看器填充文件中的表格。 但是,然后,我想阅读存储在文件中的表格,提取数据,并且它不起作用。
h5file = open_file("DCloneTable.h5", mode="a")
table = h5file.root.DCloneEntries.Entries
particle = table.row
"""???"""
h5file.close()
我尝试使用粒子[“ user_name”](因为未定义'user_name'),它给了我“ b''”作为输出
h5file = open_file("DCloneTable.h5", mode="a")
table = h5file.root.DCloneEntries.Entries
particle = table.row
print(f'{particle["user_name"]}')
h5file.close()
b''
,如果我这样做
h5file = open_file("DCloneTable.h5", mode="a")
table = h5file.root.DCloneEntries.Entries
particle = table.row
print(f'{particle["user_name"]} - {particle["game_name"]} - {particle["game_pswd"]}')
h5file.close()
b'' - b'' - b''
我在哪里失败?非常感谢:)
I am trying to write a Discord bot in Python. Goal of that bot is to fill a table with entries from users, where are retrieved username, gamename and gamepswd. Then, for specific users to extract these data and remove the solved entry. I took first tool found on google to manage tables, therefore PyTables, I'm able to fill a table in a HDF5 file, but I am unable to retrieve them.
Could be important to say I never coded in Python before.
This is how I declare my object and create a file to store entries.
class DCParties (tables.IsDescription):
user_name=StringCol(32)
game_name=StringCol(16)
game_pswd=StringCol(16)
h5file = open_file("DCloneTable.h5", mode="w", title="DClone Table")
group = h5file.create_group("/", 'DCloneEntries', "Entries for DClone runs")
table = h5file.create_table(group, 'Entries', DCParties, "Entrées")
h5file.close()
This is how I fill entries
h5file = open_file("DCloneTable.h5", mode="a")
table = h5file.root.DCloneEntries.Entries
particle = table.row
particle['user_name'] = member.author
particle['game_name'] = game_name
particle['game_pswd'] = game_pswd
particle.append()
table.flush()
h5file.close()
All these work, and I can see my entries fill the table in the file with an HDF5 viewer.
But then, I wish to read my table, stored in the file, to extract datas, and it's not working.
h5file = open_file("DCloneTable.h5", mode="a")
table = h5file.root.DCloneEntries.Entries
particle = table.row
"""???"""
h5file.close()
I tried using particle["user_name"] (because 'user_name' isn't defined), it gives me "b''" as output
h5file = open_file("DCloneTable.h5", mode="a")
table = h5file.root.DCloneEntries.Entries
particle = table.row
print(f'{particle["user_name"]}')
h5file.close()
b''
And if I do
h5file = open_file("DCloneTable.h5", mode="a")
table = h5file.root.DCloneEntries.Entries
particle = table.row
print(f'{particle["user_name"]} - {particle["game_name"]} - {particle["game_pswd"]}')
h5file.close()
b'' - b'' - b''
Where am I failing ? Many thanks in advance :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一种简单的方法,可以在表行上迭代并在时间上打印一个。
HDF5不支持Unicode字符串,因此您的角色数据被存储为字节字符串。这就是为什么您会看到
'B'
。要摆脱'b'
,您必须使用.decode('utf-8'')
使用.decode('utf-8')。这与您的硬编码字段名称一起使用。您可以使用
)避免打开文件。table.colnames
的值来处理任何列名。另外,我建议使用Python的文件上下文管理器(使用/as:如果您希望一次读取所有数据,则可以使用
table.read()
方法将数据加载到numpy结构化数组中。您仍然必须从字节转换为Unicode。结果,它“稍微复杂一些”,因此我没有发布该方法。Here is a simple method to iterate over the table rows and print them one at time.
HDF5 doesn't support Unicode strings, so your character data is stored as byte strings. That's why you see the
'b'
. To get rid of the'b'
, you have to convert back to Unicode using.decode('utf-8')
. This works with your hard coded field names. You could use the values fromtable.colnames
to handle any column names. Also, I recommend using Python's file context manager (with/as:
) to avoid leaving a file open.If you prefer to read all the data at one time, you can use the
table.read()
method to load the data into a NumPy structured array. You still have to convert from bytes to Unicode. As a result it is "slightly more complicated", so I didn't post that method.