Search code examples
pythonpython-3.xpython-2.7hdf5

python 2 created h5 file data into python 3


I am in the progress of transferring some piece of code from python 2 (2.7) to python 3 (3.7 or later)

However this piece of code reads a h5 file which was created by code in python 2.7. This piece of code will also be transferred to python 3, but not by me. I need the data in the h5 file to check whether the conversion to python 3 on my end works well (internally the data is a pandas dataframe).

Therefore I am looking for a trick (using either python 2 or python 3) to convert this h5 file into something that I can than read with python 3. It does not need to be a neat solution since it will only be temporarily.

The data is rather sizable.


Solution

  • So what I ended up doing is using python 2 to read the h5 and storing it as a json (one per key in the h5)

    Then I used a python 3 script to read the jsons and store them as an h5 file again

    (in python 2)
    foo = pandas.read_hdf('file.h5', key='bla', mode='r')
    foo.to_json('file.json')
    
    (in python 3)
    foo = pandas.read_json('file.json')
    foo.to_hdf('file2.h5', key='bla', mode='w')
    

    So it ended up being rahter simple. Hopefully this asnwer will help someone being stuck with the same.