簡體   English   中英

有沒有辦法將二進制文件存儲到 HDF5?

[英]Is there a way to store a binary file to HDF5?

我有一個 numpy .npy文件,想將它存儲在我的 HDF 中。 我使用 numpy 格式,因為該文件具有 dtype U22H5py不喜歡這樣。

有沒有辦法將此二進制文件存儲在 HDF 中,以便我可以使用像file['general/numpy_binary']這樣的字典格式來訪問它。 如果需要,我可以嘗試提供 MWE。

MWE:

a = np.array([['here',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 表。 那可能嗎?

你所展示的似乎是你的工具的一個錯誤。 觀察:

>>> 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)。 兩行都存在於文件中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM