Converting hdf5 to csv or tsv files

前端 未结 5 1522
情话喂你
情话喂你 2021-01-02 07:20

I am looking for a sample code which can convert .h5 files to csv or tsv. I have to read .h5 and output should be csv or tsv.

Sample code would be much appreciated,p

5条回答
  •  -上瘾入骨i
    2021-01-02 07:39

    Another python solution using pandas.

    #!/usr/bin/env python3
    
    import pandas as pd
    import sys
    fpath = sys.argv[1]
    if len(sys.argv)>2:
        key = sys.argv[2]
        df = pd.read_hdf(fpath, key=key)
    else:
        df = pd.read_hdf(fpath)
    
    df.to_csv(sys.stdout, index=False)
    

    This script is available here

    First argument to this scrpt is hdf5 file. If second argument is passed, it is considered to be the name of column otherwise all columns are printed. It dumps the csv to stdout which you can redirect to a file.

    For example, if your data is stored in hdf5 file called data.h5 and you have saved this script as hdf2df.py then

    $ python3 hdf2df.py data.hf > data.csv
    

    will write the data to a csv file data.csv.

提交回复
热议问题