6
$\begingroup$

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?

$\endgroup$

3 Answers 3

7
$\begingroup$

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.

$\endgroup$
2
  • 1
    $\begingroup$ 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... $\endgroup$ Commented Sep 27, 2014 at 19:39
  • $\begingroup$ @Albert I added what you suggested to my answer. I also added a link to the page where these formats are documented. $\endgroup$
    – Mr.Wizard
    Commented Sep 28, 2014 at 2:20
5
$\begingroup$

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|>}

$\endgroup$
4
  • $\begingroup$ 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. $\endgroup$
    – Mr.Wizard
    Commented Sep 28, 2014 at 20:47
  • 1
    $\begingroup$ @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. $\endgroup$ Commented Sep 28, 2014 at 23:31
  • $\begingroup$ @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. $\endgroup$
    – mete
    Commented Sep 29, 2014 at 14:58
  • $\begingroup$ @Rolf Mertig: I cannot agree more with you about .mx file import/export speed. It is extremely fast compared to csv, xls, dat formats. $\endgroup$ Commented Feb 1, 2021 at 14:27
0
$\begingroup$

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

$\endgroup$
1
  • $\begingroup$ But I don't know in advance how many I will have. I am generating them and putting into a file $\endgroup$
    – mete
    Commented Sep 27, 2014 at 17:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.