在存储hdf5文件时出现错误,其中包含一个字符串列表。

问题描述 投票:0回答:1
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"。

但我观察到的hdf5文件是这样的。enter image description here

谁能告诉我我做错了什么?谢谢大家

python hdf5
1个回答
2
投票

我不太清楚为什么你在你的文件中没有得到两个字段 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)  

其他想法观察。

  1. 我注意到你将时间值存储为一个字符串。这是你想要的吗?HDF5和h5py可以在每个fieldcolumn中存储不同的数据类型,所以如果你想的话,你可以混合浮动和和字符串。这需要不同的dtype(像记录数组)。
  2. 你使用FLAGS_F作为标志来创建 AcqFlags 数据集。你可以简化这个测试是否存在,或者使用 require_dataset.
  3. 你一次只添加一行到一个可调整大小的数据集中。这对于 "小 "数据集来说是可以的,但是如果你一次创建10e6行的数据集,就会出现性能问题。
  4. 如果你有兴趣,我回答了其他问题,展示了如何做上面的#2和#3。你可能会发现这些答案中的一个对你有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.