# Simple way to save several variables

**URL:** https://discourse.julialang.org/t/simple-way-to-save-several-variables/42549
**Category:** New to Julia
**Tags:** question
**Created:** [July 5, 2020, 11:16am UTC](https://discourse.julialang.org/t/simple-way-to-save-several-variables/42549 "2020-07-05T11:16:43Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)
#### Post date: [July 5, 2020, 11:16am UTC](https://discourse.julialang.org/t/simple-way-to-save-several-variables/42549/1 "2020-07-05T11:16:43Z")

</div>

Although I have been using Julia for over a year now, I still have problems with basic things, so I consider myself to be a ‘newbie’, so please be gentle.

I would like a simple way for me (and my students) to be able to save a few variables to the same file, and read them in again.

There have been many suggestions on here, and this seems to evolve all the time, but I can’t seem to find a simple answer.

Suppose there are arrays A1 and A2

I would like something like

write(filename, A1, A2)

read(filename)

Or even to be able to read and write a dictionary of variables

dict1=Dict(“A1”=\>A1,“A2”=\>A2)

write(filename,dict1)

dict2=read(filename)

I find it surprising that after all the time and work which has been devoted  
to the Julia project, there is no clear and simple solution to this (or at least  
one I could find).  
PS. MAT seemed to do what I wanted, but the files are huge (even with compression) and anyway, it doesn’t seem to be supported any more (or at least it doesn’t work for me).

Thanks for any help.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [July 5, 2020, 11:29am UTC](https://discourse.julialang.org/t/simple-way-to-save-several-variables/42549/2 "2020-07-05T11:29:08Z")

</div>

> [@compleat](#):
>
> arrays A1 and A2

If the element types are “simple” (eg `Float64`, `Int`), use JSON or HDF5. Both support multiple variables in the same file, hence a `Dict` can be saved.

> [@compleat](#):
>
> surprising that after all the time and work which has been devoted  
> to the Julia project, there is no clear and simple solution to this

Given the complexity of Julia’s type system, I don’t think that there will every be a “simple” solution for the very general case, but the basics are fine.

---

<div class="post-metadata">

### Author: ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)
#### Post date: [July 5, 2020, 11:40am UTC](https://discourse.julialang.org/t/simple-way-to-save-several-variables/42549/3 "2020-07-05T11:40:56Z")

</div>

Thanks for that. So DataFrames can’t be included, I guess.  
What about a collection of DataFrames? I guess that is not possible?

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [July 5, 2020, 11:45am UTC](https://discourse.julialang.org/t/simple-way-to-save-several-variables/42549/4 "2020-07-05T11:45:20Z")

</div>

> [@compleat](#):
>
> DataFrames can’t be included, I guess

No, they can be saved to either of the formats above with a little conversion.

It would be great to have an MWE generating the kind of data you want to save to focus the discussion.

---

<div class="post-metadata">

### Author: ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)
#### Post date: [July 5, 2020, 11:52am UTC](https://discourse.julialang.org/t/simple-way-to-save-several-variables/42549/5 "2020-07-05T11:52:21Z")

</div>

What is an MWE? If in my example above, A2 were a DataFrame, for example? That would be an example with as much generality as I would envision. Pickle can handle DataFrames in python. Maybe I am thinking about a Julia equivalent of pickle.  
(though I know Julia is not python, some concepts are obviously ‘inspired’ by pythonian ones)

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [July 5, 2020, 12:02pm UTC](https://discourse.julialang.org/t/simple-way-to-save-several-variables/42549/6 "2020-07-05T12:02:04Z")

</div>

> [@compleat](#):
>
> What is an MWE?

A minimal working example that generates your data.

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757/):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

> [@compleat](#):
>
> If in my example above, A2 were a DataFrame, for example?

This is still an underspecified problem — a `DataFrame` can contain arbitrary objects.

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [July 5, 2020, 12:07pm UTC](https://discourse.julialang.org/t/simple-way-to-save-several-variables/42549/7 "2020-07-05T12:07:31Z")

</div>

For specifically DataFrames, you can try also [JDF](https://github.com/xiaodaigh/JDF.jl), a DataFrame serialisation format…

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [July 5, 2020, 12:10pm UTC](https://discourse.julialang.org/t/simple-way-to-save-several-variables/42549/8 "2020-07-05T12:10:50Z")

</div>

If the goal is to store information for a short period and you don’t need the file to be human readable, you can use `serialize`:

```julia
julia> using Serialization

julia> d = Dict(3 => 4)
Dict{Int64,Int64} with 1 entry:
  3 => 4

julia> serialize("hello.txt", d)

julia> d2 = deserialize("hello.txt")
Dict{Int64,Int64} with 1 entry:
  3 => 4

```

---

<div class="post-metadata">

### Author: ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)
#### Post date: [July 5, 2020, 12:28pm UTC](https://discourse.julialang.org/t/simple-way-to-save-several-variables/42549/9 "2020-07-05T12:28:32Z")

</div>

Hi This looks really cool. Thanks for that!

---

<div class="post-metadata">

### Author: ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)
#### Post date: [July 5, 2020, 12:29pm UTC](https://discourse.julialang.org/t/simple-way-to-save-several-variables/42549/10 "2020-07-05T12:29:10Z")

</div>

Hi, This looks really cool, too!

Thanks!

---

<div class="post-metadata">

### Author: ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)
#### Post date: [July 5, 2020, 2:24pm UTC](https://discourse.julialang.org/t/simple-way-to-save-several-variables/42549/11 "2020-07-05T14:24:19Z")

</div>

I concur with the suggestion to use `serialize` for short-term storage - basically, when you are in principle OK with the file becoming unreadable after updating Julia. To store data in the long-term it’s better to write as a “common” format, not a julia-specific one: csv/JSON/BSON/database/parquet/hdf/… Note that this is also true when using other languages, e.g. python: I myself experienced several times that `pickle` couldn’t read the data back when some packages got updated.
