存储包含字符串列表的hdf5文件时出错

2024-07-01 07:23:49 发布

您现在位置:Python中文网/ 问答频道 /正文

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\u name,t0,text,ID)。标志F最初为0且ID=“122”

但我观察的hdf5文件如下: enter image description here

有人能指出我做错了什么吗?谢谢大家!


Tags: textnameiddatatimenpcontentarray
1条回答
网友
1楼 · 发布于 2024-07-01 07:23:49

(我)不清楚为什么在AcqFlags数据集中没有两个字段。我能够让您的代码段通过一个小的修改来工作。(我使用的是h5py 2.9.0。在h5py 2.10.0中为可变长度字符串添加了一个新的数据类型。在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可以在每个字段/列中存储不同的数据类型,因此如果需要,可以混合使用浮点和字符串。它需要不同的数据类型(如记录数组)
  2. 您可以使用标志作为标志来创建AcqFlags数据集。您可以将其简化为测试是否存在,或者使用require_dataset
  3. 一次向可调整大小的数据集添加一行。这对于“较小”的数据集是可以的,但如果一次创建一行10e6行,则可能会出现性能问题
  4. 如果您感兴趣,我回答了其他问题,说明如何执行上面的第2和第3项。您可能会发现以下答案之一很有帮助:

相关问题 更多 >

    热门问题