1
$\begingroup$

I produce several times automatically a number of different lists of varying length and structure like:

listA = {{100, 1.7*^10, 2.08*^9}, {105, 1.91*^10, 1.02*^9}, {110, 
2.5*^10, 3.08*^9}};
listB = {{126, 27.6, 3.19}, {131, 29.21, 3.2}};
listC = {3.84, 2.9, 5.90, 4.2, 1.3, 0.91, 1.19, 1.5, 2.79, 2.9};

Those I export via: Export["lists.txt", {listA, listB, listC}, "Table"]. I know, that I can import them as a whole Import["lists.txt", "Table"] but I would like to automatically import them list-wise similar to the following fantasy code:

list1 = Import["lists.txt", listA]
list2 = Import["lists.txt", listB]
list3 = Import["lists.txt", listC]

But this doesn't work. And since my lists have varying length, I can't use Part after the import process, since I don't know which length the listA has in the specific case.

Is there an import procedure, which can be used? Do I have to use another export method in order to enable an easier import?

$\endgroup$
3
  • 1
    $\begingroup$ what is wrong with {list1, list2, list3} = Import["lists.txt", "Table"]? $\endgroup$
    – kglr
    Jan 26, 2019 at 11:46
  • $\begingroup$ you don't need to know the lengths of individual lists to use Part, that is, imp=Import["lists.txt","Table"]; imp[[1]] is just fine to get the first list. Similarly, for imp[[2]], imp[[3]]. $\endgroup$
    – kglr
    Jan 26, 2019 at 11:49
  • $\begingroup$ @kglr Perfect, those are two solutions at once. The first one is exactly, what I searched for. In case you make it an answer, I'll accept it. $\endgroup$
    – Lea
    Jan 26, 2019 at 12:04

1 Answer 1

2
$\begingroup$

You can use

{list1, list2, list3} = Import["lists.txt", "Table"]

Alternatively,

imp = Import["lists.txt", "Table"];
list1 = imp[[1]]
list2 = imp[[2]]

etc.

$\endgroup$
2
  • $\begingroup$ there is a problem with this method since in the lists A and B the commas get doubled during import. Independent which of those methods I use, I get for imp[[2]]: {"{126,", "27.6,", "3.19}", "{131,", "29.21,", "3.2}"}. Has this something to do with the "Table"? $\endgroup$
    – Lea
    Jan 27, 2019 at 9:25
  • $\begingroup$ Ah, I found something here mathematica.stackexchange.com/questions/16141/… But for me it is not really satisfying, why does it not work with txt? $\endgroup$
    – Lea
    Jan 27, 2019 at 9:44

Your Answer

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

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