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")
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.
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.
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