我需要使用 h5py
在 HDF5 文件中存储字符串列表/数组。这些字符串的长度是可变的。按照我在网上找到的示例,我有一个可以运行的脚本。
import h5py
h5File=h5py.File('outfile.h5','w')
data=['this','is','a','sentence']
dt = h5py.special_dtype(vlen=str)
dset = h5File.create_dataset('words',(len(data),1),dtype=dt)
for i,word in enumerate(data):
dset[i] = word
h5File.flush()
h5File.close()
但是,当数据
变得非常大时,写入需要很长时间,因为它循环遍历每个条目并将其插入到文件中。
我认为我可以在一行中完成所有操作,就像使用整数或 float 一样。但以下脚本失败。请注意,我添加了一些代码来测试 int
是否有效。
import h5py
h5File=h5py.File('outfile.h5','w')
data_numbers = [0, 1, 2, 3, 4]
data = ['this','is','a','sentence']
dt = h5py.special_dtype(vlen=str)
dset_num = h5File.create_dataset('numbers',(len(data_numbers),1),dtype=int,data=data_numbers)
print("Created the dataset with numbers!\n")
dset_str = h5File.create_dataset('words',(len(data),1),dtype=dt,data=data)
print("Created the dataset with strings!\n")
h5File.flush()
h5File.close()
该脚本给出以下输出。
Created the dataset with numbers!
Traceback (most recent call last):
File "write_strings_to_HDF5_file.py", line 32, in <module>
dset_str = h5File.create_dataset('words',(len(data),1),dtype=dt,data=data)
File "/opt/anaconda3/lib/python3.7/site-packages/h5py/_hl/group.py", line 136, in create_dataset
dsid = dataset.make_new_dset(self, shape, dtype, data, **kwds)
File "/opt/anaconda3/lib/python3.7/site-packages/h5py/_hl/dataset.py", line 170, in make_new_dset
dset_id.write(h5s.ALL, h5s.ALL, data)
File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
File "h5py/h5d.pyx", line 211, in h5py.h5d.DatasetID.write
File "h5py/h5t.pyx", line 1652, in h5py.h5t.py_create
File "h5py/h5t.pyx", line 1713, in h5py.h5t.py_create
TypeError: No conversion path for dtype: dtype('<U8')
我已经阅读了有关 UTF-8 编码的文档,并尝试了上述语法的多种变体,但我似乎遗漏了一些关键点。或许做不到?
感谢任何提出建议的人!
如果有人想看看示例中的减速情况是否有效,这里有一个测试用例。
import h5py
h5File=h5py.File('outfile.h5','w')
sentence=['this','is','a','sentence']
data = []
for i in range(10000):
data += sentence
print(len(data))
dt = h5py.special_dtype(vlen=str)
dset = h5File.create_dataset('words',(len(data),1),dtype=dt)
for i,word in enumerate(data):
dset[i] = word
h5File.flush()
h5File.close()
最佳答案
一次写入 1 行数据是写入 HDF5 文件的最慢方法。当写入 100 行时,您不会注意到性能问题,但随着行数的增加,您会看到它。还有另一个答案讨论了这个问题。看到这个:pytables writes much faster than h5py. Why? (注意:我不建议您使用 PyTables
。链接的答案显示 h5py
和 PyTables
的性能)。正如你所看到的,当写入大量小块时,写入相同数量的数据需要更长的时间。
为了提高性能,您需要每次写入更多数据。由于您已加载列表 data
中的所有数据,您可以一举完成。 10,000 行几乎是瞬时的。评论中引用的答案涉及这种技术(从列表数据创建np.array()
。但是,它从小列表(1/行)开始工作......所以不完全相同。创建时必须小心数组。您不能使用 NumPy 的默认 Unicode dtype - h5py
不支持它。相反,您需要 dtype='S#'
下面的代码显示将字符串列表转换为 np.array()
字符串。另外,我强烈建议您使用Python的with/as:
连接管理器打开文件。这可以避免由于意外退出(由于崩溃或逻辑错误)而导致文件意外保持打开状态的情况。
代码如下:
import h5py
import numpy as np
sentence=['this','is','a','sentence']
data = []
for i in range(10_000):
data += sentence
print(len(data))
longest_word=len(max(data, key=len))
print('longest_word=',longest_word)
dt = h5py.special_dtype(vlen=str)
arr = np.array(data,dtype='S'+str(longest_word))
with h5py.File('outfile.h5','w') as h5File:
dset = h5File.create_dataset('words',data=arr,dtype=dt)
print(dset.shape, dset.dtype)
关于python - 我可以使用 h5py 在一行中将字符串写入 HDF5 文件,而不是循环遍历条目吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68500454/