Reputation: 107
Is there a standard way to easily just save any kind of data in Julia. This time I have a tuple with two arrays in it that I want to use again later. Something like this.
a = [1,2,34]
b = [1,2,3,4,5]
save((a,b), "ab")
a, b = load("ab")
Upvotes: 6
Views: 8377
Reputation: 7684
You can use JLD2.jl to save and load arbitrary Julia objects. This is more likely to work for long term storage across different Julia versions than the Serialization standard library.
using JLD2
x = (1, "a")
save_object("mytuple.jld2", x)
julia> load_object("mytuple.jld2")
(1, "a")
For additional functionality, check out the docs.
Upvotes: 11
Reputation: 42234
There are many scenarios depending on how long you want to store the data
julia> using Serialization
julia> serialize("file.dat",a)
24
julia> deserialize("file.dat")
3-element Vector{Int64}:
1
2
34
julia> using DelimitedFiles
julia> writedlm("file.csv", a)
julia> readdlm("file.csv", '\t' ,Int)
3×1 Matrix{Int64}:
1
2
34
julia> using JSON3
julia> JSON3.write("file.json",a)
"file.json"
julia> open("file.json") do f; JSON3.read(f,Vector{Int}); end
3-element Vector{Int64}:
1
2
34
Other worth mentioning libraries (depending on data format) include CSV.jl
for saving data frames and BSON.jl
for binary JSON files.
Upvotes: 6