1
$\begingroup$

I have a set of numbers in notepad that I want to import into Mathematica as a list. I could do this manually, but it will take forever, and my hands are getting tired. My issue is that I'm trying to get these numbers into mathematica, but when I copy and paste everything into mathematica, there are multiplication symbols in between the spaces. Aside from having to delete all those symbols, I would have to also add commas. There are a lot numbers and it isn't practical.

I would like the end result to look like this (using arbitrary numbers):{310,8,597,15,898,6,700,894,7,175,170,2,1673,2,967,3,158,10,6,1095,12,5,193,97,163}

EDIT: I was able to import the raw data (without any multiplication symbols). All I did was call Import[(file path here)]. It looks sort of like this now (sample data using arbitrary numbers):

43 454 757 34 90 34 74 43 86 23 34 65 03 54 51 65 6 52 64 54 54 65 74 54 50

Now I just need to turn this numbers into a list.

$\endgroup$
  • $\begingroup$ Please edit your question and post your code and sample data. There are lots of very knowledgeable people here, but they need more info to know what you want. Ideally, a good question and the answers it generates are useful to many others besides yourself. $\endgroup$ – dionys Jun 7 '16 at 13:10
  • $\begingroup$ Welcome! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – user9660 Jun 7 '16 at 14:07
4
$\begingroup$

Make some sample data consisting of numbers and spaces:

samepleData = Module[{seed = ToString@Table[RandomInteger[{0, 10000}], 50]},
                StringReplace[seed, {"{" -> " ",
                                     "}" -> " ",
                                     "," -> RandomChoice[{" ", "  ", "  "}]}]]

(* 7476  8829  1505  3337  7282  7244  6783  3389  7608  7031  4033  1579  4323  1695  2325  1821  9879  4811  5464  563  7065  8359  8860  8119  3434  8838  6348  9036  8166  2467  2939  4287  7551  2184  4126  9049  8925  5883  1863  9815  7154  2806  3746  9096  7972  3188  4174  8206  9813  1281 *)

Mathematica's built-in import functions can handle this format directly:

ImportString[samepleData, "Words"]
(*{7476,8829,1505,3337,7282,7244,6783,3389,7608,7031,4033,1579,4323,1695,2325,1821,9879,4811,5464,563,7065,8359,8860,8119,3434,8838,6348,9036,8166,2467,2939,4287,7551,2184,4126,9049,8925,5883,1863,9815,7154,2806,3746,9096,7972,3188,4174,8206,9813,1281}*)

If your input is a file, you can use Import instead of ImportString. Note that they work the same way with exactly the same element specifications and options.

Also, spaces normally mean multiplication in Mathematica syntax. This may be why you see "multiplication symbols in between the spaces" with some import methods when numbers separated by spaces are interpreted as an expression.

$\endgroup$
  • $\begingroup$ Awesome! This worked, thank you. $\endgroup$ – CrypticParadigm Jun 7 '16 at 13:53
1
$\begingroup$

Szabolcs Horvát has a great bit of code that makes bring in tabular data via copy and paste super easy:

Pasting tabular data from the web http://szhorvat.net/pelican/pasting-tabular-data-from-the-web.html

$\endgroup$
0
$\begingroup$

I cut and paste the numbers you have to a .csv file, importing them and was left with the numbers in Text format. Try this.

ToExpression[Flatten[StringSplit[Flatten[Import["C:\\File.csv"]]]]]

It flattens and converts it to a expression, numbers instead of text format.

$\endgroup$

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.