[Python] How can extract data from .h5 file and save it in .txt or .csv properly?

Discussão em 'Python' iniciado por Stack, Dezembro 1, 2024.

  1. Stack

    Stack Membro Participativo

    After searching a lot I couldn't find a simple way to extract data from .h5 and pass it to a data.Frame by Numpy or Pandas in order to save in .txt or .csv file.

    import h5py
    import numpy as np
    import pandas as pd

    filename = 'D:\data.h5'
    f = h5py.File(filename, 'r')

    # List all groups
    print("Keys: %s" % f.keys())
    a_group_key = list(f.keys())[0]

    # Get the data
    data = list(f[a_group_key])
    pd.DataFrame(data).to_csv("hi.csv")


    Keys: <KeysViewHDF5 ['dd48']>


    When I print data I see following results:

    print(data)


    ['axis0',
    'axis1',
    'block0_items',
    'block0_values',
    'block1_items',
    'block1_values']


    I would appreciate the if someone explain me what are they and how I can extract data completely and save it in .csv file. It seems there hasn't been a routine way to do that and it's kind of challenging yet! Until now I just could see part of data via:

    import numpy as np
    dfm = np.fromfile('D:\data.h5', dtype=float)
    print (dfm.shape)
    print(dfm[5:])

    dfm=pd.to_csv('train.csv')
    #dfm.to_csv('hi.csv', sep=',', header=None, index=None)


    My expectation is to extract time_stamps and measurements in .h5 file.

    Continue reading...

Compartilhe esta Página