# Save associations to a file and reload

I have many (say 2 for now) associations that I want to save to a file

<|"A" -> 1 , "B" -> 2|> >>> NotebookDirectory[] <> "TEST.m"
<|"A" -> 11 , "B" -> 22|> >>> NotebookDirectory[] <> "TEST.m"


Now I would like to somehow get all this expressions into a list, obtaining

{<|"A" -> 1 , "B" -> 2|>,<|"A" -> 11 , "B" -> 22|>}


But if I do

loaded = Get[NotebookDirectory[] <> "TEST.m"]


I obtain only the last one

<|"A" -> 11 , "B" -> 22|>


What is the correct thing to do?

Your file does contain the expressions you saved but Get evaluates the file rather than importing the lines as expressions. The latter can be done with Import:

Import["TEST.m", "ExpressionList"]

{<|"A" -> 1, "B" -> 2|>, <|"A" -> 11, "B" -> 22|>}


Alternatively you could also save a definition rather than the bare expressions. See:

As Albert Retey comments I failed to mention that the behavior in this case is not related to Association but only the mechanics of PutAppend and especially Get. The same behavior would be seen for any series of standard expressions.

An additional note: the Import command above evaluates expressions (like Get) but also returns a list of them. If you wish to only return a list without evaluating at all you can use:

Import["file.m", "HeldExpressions"]


Each expression will be returned wrapped in HoldComplete.

These Import elements are described in the documentation for the Package format.

• thanks, that's very useful, wouldn't hav had "ExpressionList" on my mind. You might want to add that that problem hasn't anything to do with the fact that the OP is saving associations, he'd have had the very same problem with PutAppend lists or even just numbers... – Albert Retey Sep 27 '14 at 19:39
• @Albert I added what you suggested to my answer. I also added a link to the page where these formats are documented. – Mr.Wizard Sep 28 '14 at 2:20

The by far fastest method to store and retrieve any data in Mathematica is to use the .mx format:

asso = {<|"A" -> 1, "B" -> 2|>, <|"A" -> 11, "B" -> 22|>};
Export["myasso.mx", asso]


The really great news about .mx is that, contrary to this 10.0 documentation, you can since Mathematica 10.0 exchange them between different operating systems, i.e. if you read the above file in under e.g. Linux by Import["myasso.mx"] you get back {<|"A" -> 1, "B" -> 2|>, <|"A" -> 11, "B" -> 22|>}

• I'm giving a +1 for this for continued dissemination of important information, however I do not believe this is applicable to the OP's needs as I believe he needs incremental output, i.e. PutAppend. – Mr.Wizard Sep 28 '14 at 20:47
• @Mr.Wizard I am no friend of PutAppend. People should use Reap / Sow . Even if the list of associations grows larger than available RAM, I would (and once did) construct large sub-lists being saved as .mx files. So much faster than anything else. – Rolf Mertig Sep 28 '14 at 23:31
• @Mr.Wizard you are correct, my input is incremental. rolf-mertig interesting approach with reap+saw+mx. I will think about that for larger dataset. – mete Sep 29 '14 at 14:58

I´ve put the associations in a Dataset and saved it via:

exp = Export["file.m", dataset]


Reading the data in is done via

dataset = Import["file"]


this works without problems, also for associations (without converting to a Dataset).

• But I don't know in advance how many I will have. I am generating them and putting into a file – mete Sep 27 '14 at 17:09