3
$\begingroup$

I have a large list (approx 50 millions elements).

I could Export it as a table like this:

Export["path/to/file.dat",list,"Table"]

but this takes a large amount of memory. Are there any alternatives for writing my list on a file as a table, which use less RAM?

$\endgroup$
1
  • 3
    $\begingroup$ Would other target formats also take up as much memory? Eg HDF5, NASACDF, ... $\endgroup$
    – MarcoB
    Commented Dec 1, 2023 at 11:47

1 Answer 1

3
$\begingroup$

You can do a batched export using an output stream. For example:

batchedExport[file_, data_List, format_, n_Integer] := Module[{
   stream = OpenAppend[file]
   },
  WithCleanup[
   Scan[
    Function[
     WriteString[
      stream,
      ExportString[#, format] <> "\n"
      ]
     ],
    Partition[data, UpTo[n]]
    ];
   file
   ,
   Close[stream]
   ]
 ];

data = RandomChoice[{1, 2., "a"}, {100000, 4}];
batchedExport["test.dat", data, "Table", 100]

You can use n to change the amount of data that gets processed each step.

$\endgroup$

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.