I have a database created in Julia with the JLD Package
This database contains 2 elements: input and output
In julia, I can load it with the following code:
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
I would like to load these arrays in Python. I tried the following (in Python):
filename = "file_path"
data = open(filename, r)
data returns the following:
data
<_io.TextIOWrapper name='file_path' mode='r' encoding='UTF-8'>
After that, I read a document that explained how to read a file. Nevertheless, if I run the following:
print(data.readlines())
I have only this output:
[]
Is it possible to load these arrays in Python ?
EDIT
I tried to do an equivalent of Julia:
data = open("file_path")["input"]
But there is this error:
TypeError: '_io.TextIOWrapper' object is not subscriptable
Maybe there is an other function to read a file ?
JLD is a specific "dialect" of HDF5, a cross-platform, multi-language data storage format most frequently used for scientific data. By comparison with "plain" HDF5, JLD files automatically add attributes and naming conventions to preserve type information for each object.
JLD2 saves and loads Julia data structures in a format comprising a subset of HDF5, without any dependency on the HDF5 C library. It typically outperforms the JLD package (sometimes by multiple orders of magnitude) and often outperforms Julia's built-in serializer.
Julia JLD files have HDF5 format so you can read them using e.g. h5py
like this:
import h5py
f = h5py.File("filename", "r")
f["input"].value, f["output"].value
The file will also contain an entry _creator
with metadata saved by Julia.
Note that Julia stores data in column major order, as opposed to row major used by numpy
, so if you would read matrices this way they would be transposed.
I had a similar issue to solve and I did the following:
I read the JLD file in julia:
data = load("filename_julia.jld")["data"]
Converted into an array by numpy.asarray using PyCall to invoke numpy,
using PyCall:
np = pyimport("numpy")
data = np.asarray(data)
I then saved the array as .npy file using numpy.save:
np.save("filename_python.npy",data)
You can then open this file in python as a regular .npy file
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With