def storeFlagsFile(FLAGS_F, file_name, t0, text, ID):
if not FLAGS_F: # this flag doesnt work for mulitple users
f = h5py.File(file_name, "r+")
data_content = np.array([np.round(time.time() - t0, 3), text])
asciiList = np.array([str(n).encode("utf-8", "ignore") for n in data_content]).reshape(1, 2)
dt = h5py.string_dtype(encoding='utf-8')
dset = f[str(ID)].create_dataset('AcqFlags', data=asciiList, compression="gzip", chunks=True, maxshape=(None, 2), dtype=dt)
FLAGS_F = 1
else:
f = h5py.File(file_name, "r+")
data_content = np.array([np.round(time.time() - t0, 3), text])
asciiList = np.array([str(n).encode("utf-8", "ignore") for n in data_content]).reshape(1, 2)
f[str(ID)+'/AcqFlags'].resize((f[str(ID)+'/AcqFlags'].shape[0] + 1), axis = 0)
f[str(ID)+'/AcqFlags'][-1:] = asciiList
我想以(None,2)格式保存这种数据格式,因为我通过调用storeFlagsFile函数不断更新每行的数据行。
['4.412' 'a']
['5.412' 'b']
['6.412' 'c']
['8.226' 'd']
其中t0的第一列和文本=数据的第二列,我将其作为每行的输入行提供给storeFlagsFile(FLAGS_F,file_name,t0,text,ID)。 FLAGS_F最初为0,ID =“ 122”。
有人可以指出我做错了什么吗?谢谢!
答案 0 :(得分:2)
(对我来说)尚不清楚为什么您的AcqFlags
数据集中没有2个字段。我能够对您的代码段进行一些小的修改。 (我使用的是h5py 2.9.0。对于可变长度字符串,在h5py 2.10.0中添加了新的dtype。该更改在dt=
声明中带有注释。这在您的代码中不是错误。)下面。
import h5py, numpy as np
import time
def storeFlagsFile(FLAGS_F, file_name, t0, text, ID):
if not FLAGS_F: # this flag doesnt work for mulitple users
with h5py.File(file_name, "r+") as f:
data_content = np.array([np.round(time.time() - t0, 3), text])
asciiList = np.array([str(n).encode("utf-8", "ignore") for n in data_content]).reshape(1, 2)
#dt = h5py.string_dtype(encoding='utf-8') # for h5py 2.10.0
dt = h5py.special_dtype(vlen=str) # for h5py 2.9.0
dset = f[str(ID)].create_dataset('AcqFlags', data=asciiList, compression="gzip", chunks=True, maxshape=(None, 2), dtype=dt)
FLAGS_F = 1
else:
with h5py.File(file_name, "r+") as f:
data_content = np.array([np.round(time.time() - t0, 3), text])
asciiList = np.array([str(n).encode("utf-8", "ignore") for n in data_content]).reshape(1, 2)
f[str(ID)+'/AcqFlags'].resize((f[str(ID)+'/AcqFlags'].shape[0] + 1), axis = 0)
f[str(ID)+'/AcqFlags'][-1:] = asciiList
file_name = 'SO_62064344.h5'
ID = 122
with h5py.File(file_name, 'w') as f:
f.create_group(str(ID))
storeFlagsFile(False, file_name, 4.412, 'a', ID)
storeFlagsFile(True, file_name, 5.412, 'b', ID)
storeFlagsFile(True, file_name, 6.412, 'c', ID)
storeFlagsFile(True, file_name, 8.226, 'd', ID)
storeFlagsFile(True, file_name, 9.773, 'e', ID)
其他想法/观察:
AcqFlags
数据集的标志。您可以简化此过程以测试是否存在,或使用require_dataset
。 如果您有兴趣,我回答了其他问题,这些问题表明了如何执行上面的#2和#3。您可能会发现以下答案之一很有帮助: