【问题标题】:python 2 created h5 file data into python 3python 2 创建 h5 文件数据到 python 3
【发布时间】:2021-03-08 04:57:12
【问题描述】:

我正在将一些代码从 python 2 (2.7) 转移到 python 3 (3.7 或更高版本)

然而,这段代码读取了一个由 py​​thon 2.7 中的代码创建的 h5 文件。这段代码也将被转移到 python 3,但不是我的。我需要 h5 文件中的数据来检查我端到 python 3 的转换是否正常(内部数据是 pandas 数据框)。

因此,我正在寻找一种技巧(使用 python 2 或 python 3)将此 h5 文件转换为我可以使用 python 3 读取的文件。它不需要是一个简洁的解决方案,因为它只是暂时的.

数据相当可观。

【问题讨论】:

  • 为什么不坚持使用h5py,它适用于 Python 2 和 3?如果您真的想要不同的文件格式,可以查看numpy.save()
  • 我不确定你的意思,但我的问题是 python 2 中的 unicode 创建了 h5 这在 python 3 中的工作方式不同。但是我发现了一个对我有用的技巧,我将它添加为回答

标签: python python-3.x python-2.7 hdf5


【解决方案1】:

所以我最终做的是使用 python 2 读取 h5 并将其存储为 json(h5 中每个键一个)

然后我使用 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')

所以它最终变得更加简单。希望这个答案能帮助遇到同样问题的人。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-19
    • 2020-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多