简体   繁体   中英

How to store list of list to h5 file in python

I have this:

data = [array([[ 0.5129556,  0.4993599,  0.5027999,  0.5005983]]), 
array([[ 0.5129556,  0.4993599,  0.5027999,  0.5005983,  
0.4979439, 0.4991087,  0.4982641]])]

name ='name'

filename = 'path'

I want to write this to a H5 file from python. I am getting the following error:

ValueError: could not broadcast input array from shape (4) into 
shape (1)

What I currently have is this:

 with h5py.File(filename, 'w') as hf:
        for i in data:
           hf.create_dataset(name = name, data=data, 
                            shape=len(data), dtype=float)

It appears that your data array has variable-length rows. Do you want to write the entire data to a single dataset or do you want to write each row of the data to a different dataset? In the first case you need to fix your data array to have equal length rows (and the loop in your code makes no sense) and in the latter case you need to fix the dataset name (ie, provide a unique name for each row) and set data=i ( i in your code and d in my code below):

with h5py.File(filename, 'w') as hf:
    for n, d in enumerate(data):
        hf.create_dataset(name = 'dataset{:d}'.format(n), data=d)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM