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是第一列数据,text=第二列数据,我把每行数据作为输入行给了storeFlagsFile(FLAGS_F, file_name, t0, text, ID)。FLAGS_F初始为0,ID="122"。
谁能告诉我我做错了什么?谢谢大家
我不太清楚为什么你在你的文件中没有得到两个字段 AcqFlags
数据集。我只需稍作修改,就能让你的代码段工作。(我使用的是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。你可能会发现这些答案中的一个对你有帮助。