2
$\begingroup$

I am having some slight issues with exporting and importing tables while preserving their forms: A sample code is here:

test1[1, 1] = {1, 2, 3};
test1[1, 2] = {4, 5, 6};
test1[3, 1] = {7, 8, 9};
test1[2, 1] = {100, 200, 300};
test1[2, 2] = {400, 500, 600};
test1[3, 2] = {700, 800, 900};
test = Table[test1[l, g], {l, 1, 3}, {g, 1, 2}]
Export["test.dat", test[[All, All]]];
testimport = Import["test.dat"]

OUTPUT:

{{{1, 2, 3}, {4, 5, 6}}, {{100, 200, 300}, {400, 500, 600}}, {{7, 8, 
   9}, {700, 800, 900}}}

{{"{1,", "2,", "3}", "{4,", "5,", "6}"}, {"{100,", "200,", "300}", 
  "{400,", "500,", "600}"}, {"{7,", "8,", "9}", "{700,", "800,", 
  "900}"}}

I cannot access the imported file like a regular table. For instance I have the following:

In[559]:= testimport[[1, 1]]
test[[1, 1]]

Out[559]= "{1,"
Out[560]= {1, 2, 3}

when I want to have testimport behave like test.

$\endgroup$
3
$\begingroup$

Use "Package" as the third argument in both Export and Import:

Export["test.dat", test, "Package"];
testimport = Import["test.dat", "Package"]

{{{1, 2, 3}, {4, 5, 6}}, {{100, 200, 300}, {400, 500, 600}}, {{7, 8, 9}, {700, 800, 900}}}

testimport == test

True

See: this answer by Han Solo.

$\endgroup$
  • $\begingroup$ Thanks @ Han Solo $\endgroup$ – Mudit Jain Oct 4 '18 at 20:04

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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