有没有办法将二进制文件存储到HDF5?

发布于 2025-01-29 09:24:28 字数 635 浏览 3 评论 0原文

我有一个numpy .npy文件,并希望将其存储在我的HDF中。我使用numpy格式,因为该文件具有 dtype u22 h5py 不喜欢这样。

有没有办法将此二进制文件存储在HDF中,因此我可以使用字典格式(例如file ['eneral/numpy_binary']访问它。如果需要,我可以尝试提供MWE。

MWE:

a = np.Array([[['there',1,2,3],['that',4,5,6]])。astype('s10')是一个2x4阵列。

with h5py.File('dump.h5','w') as debug:
    g=debug.create_group('general')
    dset = debug.create_dataset('general/data', data=a.tolist())

结果:

结果数据集

我希望将此数据视为2x4表。有可能吗?

I have a numpy .npy file and would like to store this in my HDF. I use the numpy format because the file has dtype U22 and H5py does not like that.

Is there a way to store this binary file inside the HDF so I can access it using the dictionary format like file['general/numpy_binary']. I can try to provide an MWE if required.

MWE:

a = np.array([['here',1,2,3], ['that',4,5,6]]).astype('S10') which is a 2x4 array.

with h5py.File('dump.h5','w') as debug:
    g=debug.create_group('general')
    dset = debug.create_dataset('general/data', data=a.tolist())

results in this:

resulting dataset

I would like to see this data as a 2x4 table. Is that possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

未央 2025-02-05 09:24:28

您所展示的似乎是工具中的错。观察:

>>> import numpy as np
>>> a = np.array([['here',1,2,3], ['that',4,5,6]]).astype('S10')
>>> a
array([[b'here', b'1', b'2', b'3'],
       [b'that', b'4', b'5', b'6']], dtype='|S10')
>>> a.tolist()
[[b'here', b'1', b'2', b'3'], [b'that', b'4', b'5', b'6']]
>>> import h5py
>>> h = h5py.File('data.h5','w')
>>> h.create_dataset('data',data=a.tolist())
<HDF5 dataset "data": shape (2, 4), type "|S4">
>>> h.close()
>>> h
<Closed HDF5 file>
>>> h = h5py.File('data.h5','r')
>>> h['data']
<HDF5 dataset "data": shape (2, 4), type "|S4">
>>> h['data'][0]
array([b'here', b'1', b'2', b'3'], dtype='|S4')
>>> h['data'][1]
array([b'that', b'4', b'5', b'6'], dtype='|S4')
>>>

请注意,形状为(2,4)。文件中都存在两个行。

What you're showing seems to be a fault in your tool. Observe:

>>> import numpy as np
>>> a = np.array([['here',1,2,3], ['that',4,5,6]]).astype('S10')
>>> a
array([[b'here', b'1', b'2', b'3'],
       [b'that', b'4', b'5', b'6']], dtype='|S10')
>>> a.tolist()
[[b'here', b'1', b'2', b'3'], [b'that', b'4', b'5', b'6']]
>>> import h5py
>>> h = h5py.File('data.h5','w')
>>> h.create_dataset('data',data=a.tolist())
<HDF5 dataset "data": shape (2, 4), type "|S4">
>>> h.close()
>>> h
<Closed HDF5 file>
>>> h = h5py.File('data.h5','r')
>>> h['data']
<HDF5 dataset "data": shape (2, 4), type "|S4">
>>> h['data'][0]
array([b'here', b'1', b'2', b'3'], dtype='|S4')
>>> h['data'][1]
array([b'that', b'4', b'5', b'6'], dtype='|S4')
>>>

Note that the shape is (2,4). Both rows are present in the file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文