I have a text file that I would like to import into Mathematica - after searching around the internet, I can't seem to find any pages that explain how to do this:

The text file containes these four lines:

1 2 3
4 5 6 7 8
1000 100

In my Mathematica code, I want to assign:

a = 10
list1 = {1, 2, 3}
list2 = {4, 5, 6, 7, 8}
b = 1000
c = 100

Is there a way to do this? I'm not very versed in Mathematica so any help will be appreciated, thank you so much in advance!

  • 1
    $\begingroup$ What have you tried? This is very basic. A quick exploration of the documentation would teach you a lot and should easily get you up to speed. Look up Import for CSV files, and then look at the list manipulation documentation pages. $\endgroup$ – user6014 Jun 24 '19 at 15:41
  • $\begingroup$ @user6014 See my answer, I don't think complicated hierarchical left-hand-sides in assignments are something that everybody knows. $\endgroup$ – Roman Jun 24 '19 at 18:24
  • $\begingroup$ @roman There's more than one way to skin a cat, though. $\endgroup$ – user6014 Jun 25 '19 at 17:47
{{a}, list1, list2, {b, c}} = Import[filename, "Table"]
(*    {{10}, {1, 2, 3}, {4, 5, 6, 7, 8}, {1000, 100}}    *)

Notice how the left-hand side mimics the structure of the right-hand side, which you can see in the output line: a nested list of lists representing the numbers on the different lines in the file.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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