简体   繁体   English

python 2 创建的 h5 文件数据到 python 3

[英]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)我正在将一些代码从 python 2 (2.7) 转移到 python 3 (3.7 或更高版本)

However this piece of code reads a h5 file which was created by code in python 2.7.然而,这段代码读取了一个由 python 2.7 中的代码创建的 h5 文件。 This piece of code will also be transferred to python 3, but not by me.这段代码也会转移到python 3,但不是由我。 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).我需要 h5 文件中的数据来检查转换到 python 3 是否正常工作(内部数据是 pandas 数据帧)。

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.因此,我正在寻找一个技巧(使用 python 2 或 python 3)将此 h5 文件转换为我可以使用 python 3 读取的文件。它不需要是一个临时的解决方案。

The data is rather sizable.数据相当可观。

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)所以我最终做的是使用 python 2 读取 h5 并将其存储为 json (h5 中的每个键一个)

Then I used a python 3 script to read the jsons and store them as an h5 file again然后我使用 python 3 脚本读取 json 并将它们再次存储为 h5 文件

(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.希望这个答案能帮助那些被困在同一问题上的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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