Message Boards Message Boards

0
|
5628 Views
|
4 Replies
|
4 Total Likes
View groups...
Share
Share this post:

Importing and exporting Tables with mixed dimensions

Posted 9 years ago

How can I export a table which looks like this:

{{{1, 2}, {4, 5}}, {5}, {1, 2, 3, 4}}

And then Import it so that the list will be the same? I tried csv and txt with "Table" and "Data" options. They just either add additional commas in wrong places or for the case of csv interprets some parts as strings.

POSTED BY: Al Guy
4 Replies
Posted 9 years ago

Thanks!

POSTED BY: Al Guy
Posted 9 years ago

Simple text works

lm = {{{1, 2}, {4, 5}}, {5}, {1, 2, 3, 4}}
SetDirectory["H:\\"];
Put[lm, "test.txt"]
newlm = << "test.txt";
lm == newlm

Result is "True"

POSTED BY: Douglas Kubler
Posted 9 years ago

Thanks. I can't do dumpsave as I need to be able to transfer files between machines.

POSTED BY: Al Guy

If you want to save Mathematica symbols and variables, it might be better to save DumpSave, as in

SetDirectory[NotebookDirectory[]];
lst = {{{1, 2}, {4, 5}}, {5}, {1, 2, 3, 4}};
DumpSave["foo.mx", lst];
Clear[lst];
Get["foo.mx"];
lst
(* {{{1, 2}, {4, 5}}, {5}, {1, 2, 3, 4}} *)

You can also use "List" but you'd have to use ToExpression. So I think mx might be better

SetDirectory[NotebookDirectory[]];
lst = {{{1, 2}, {4, 5}}, {5}, {1, 2, 3, 4}};
Export["foo.txt", lst, "List"];
lst0 = ToExpression[Import["foo.txt", "List"]];
lst === lst0
(*  True  *)
POSTED BY: Nasser M. Abbasi
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract