在 Python 中加载 JLD 文件

Jul*_*en 5 python julia

我有一个使用JLD 包在 Julia 中创建的数据库

该数据库包含 2 个元素:输入和输出

在 julia 中,我可以使用以下代码加载它:

using JLD

data_in = load("file_path")["input"]
1×5 Array{Int64,2}:
 1  2  3  4  5

data_out = load("file_path")["output"]
1×5 Array{Int64,2}:
 3  6  9  12  15
Run Code Online (Sandbox Code Playgroud)

我想在 Python 中加载这些数组。我尝试了以下(在 Python 中):

filename = "file_path"

data = open(filename, r)
Run Code Online (Sandbox Code Playgroud)

数据返回以下内容:

data
<_io.TextIOWrapper name='file_path' mode='r' encoding='UTF-8'>
Run Code Online (Sandbox Code Playgroud)

之后,我阅读了一个解释如何读取文件的文档。不过,如果我运行以下命令:

print(data.readlines())
Run Code Online (Sandbox Code Playgroud)

我只有这个输出:

[]
Run Code Online (Sandbox Code Playgroud)

是否可以在 Python 中加载这些数组?

编辑

我试图做一个相当于朱莉娅的:

data = open("file_path")["input"]
Run Code Online (Sandbox Code Playgroud)

但是有这个错误:

TypeError: '_io.TextIOWrapper' object is not subscriptable
Run Code Online (Sandbox Code Playgroud)

也许还有其他功能可以读取文件?

Bog*_*ski 5

Julia JLD 文件具有 HDF5 格式,因此您可以使用h5py如下方式读取它们:

import h5py
f = h5py.File("filename", "r")
f["input"].value, f["output"].value
Run Code Online (Sandbox Code Playgroud)

该文件还将包含一个包含_creatorJulia 保存的元数据的条目。

请注意,Julia 以列主要顺序存储数据,而不是 使用的行主要顺序,numpy因此如果您以这种方式读取矩阵,它们将被转置。