9

in this expression = "{1 2 3 4 5 6 7 8}", I want to replace each space by a comma , to get {1,2,3,4,5,6,7,8}.

Kuba
  • 136,707
  • 13
  • 279
  • 740
BetterEnglish
  • 2,026
  • 13
  • 19

8 Answers8

14
expression = "{1 2 3 4 5 6 7 8}";

A few alternatives to PlatoManiac's approach:

ToExpression[
    expression, 
    StandardForm, 
    Function[e, Sequence @@@ Unevaluated[e], HoldAll]
]

or

ToExpression @ StringReplace[expression, " " -> ","]
{1, 2, 3, 4, 5, 6, 7, 8} 

or

 StringCases[expression, n : NumberString :> ToExpression[n]]

 ToExpression @ StringCases[expression, NumberString]
Kuba
  • 136,707
  • 13
  • 279
  • 740
10

A possibility with WhitespaceCharacter

ToExpression@
     StringReplace[expression, WhitespaceCharacter -> ","]

(* {1, 2, 3, 4, 5, 6, 7, 8} *)

Another using Interpreter

Interpreter[
   DelimitedSequence["Integer", {"{", " ", "}"}]][expression]
8

My thought was to drop the brackets using StringTake and then import it using ImportString

"{1 2 3 4 5 6 7 8}"~StringTake~{2, -2}~ImportString~"Table" // First
(* {1, 2, 3, 4, 5, 6, 7, 8} *)

edit I just realized this is very similar to what J.M. suggested in his comment (although I'm playing around with infix notation here).

Jason B.
  • 68,381
  • 3
  • 139
  • 286
  • 1
    It's not so similar to JMs version and in fact, I was going to post exactly this solution, because I think it is noteworthy that a space-separated list of numbers can be imported as "Table". +1 – halirutan Dec 16 '15 at 08:39
  • Thanks, it was my first thought since I'm regularly running python (or c or fortran) code that outputs a big list or matrix and the quickest way for me to plot it is to just copy it and paste it into an ImportString command. – Jason B. Dec 16 '15 at 08:47
6

Since space is implicit Times similar methods to Convert head Times to List can be applied:

Block[{Times = List}, ToExpression @ "{1 2 3 4 5 6 7 8}"] // First
{1, 2, 3, 4, 5, 6, 7, 8}

Though in this case a much shorter method works too:

ToHeldExpression[expression] ~Level~ {3}
{1, 2, 3, 4, 5, 6, 7, 8}
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
5

Another solution, inspired by Kuba's first one:

Sequence @@@ ToExpression[expression, StandardForm, Inactivate] 
Fred Simons
  • 10,181
  • 18
  • 49
2

A similar case to "{1 2 3 4 5 6 7 8}", that also appears when you extract data from txt, is {"1 2 3 4 5 6 7 8"}

In that case, one can use

ToExpression@ StringSplit@ {"1 2 3 4 5 6"}
MarcoB
  • 67,153
  • 18
  • 91
  • 189
2

Another way is to use StringRiffle:

ToExpression[StringRiffle[StringSplit["{1 2 3 4 5 6 7 8}"], ","]]
{1, 2, 3, 4, 5, 6, 7, 8}

Note that StringRiffle can also perform the inverse operation too:

StringRiffle[{1, 2, 3, 4, 5, 6, 7, 8}, {"{", ", ", "}"}]
"{1, 2, 3, 4, 5, 6, 7, 8}"
Greg Hurst
  • 35,921
  • 1
  • 90
  • 136
0

Using Composition :

strToList = Composition[
  ToExpression,
  StringSplit,
  StringReplace[#,
    PunctuationCharacter :> ""] &
  ];

alist = strToList@expression

{1, 2, 3, 4, 5, 6, 7, 8}


Alternate notation:

ToExpression @*
  StringSplit@*(StringReplace[#, 
     PunctuationCharacter :> ""] &)@expression
Syed
  • 52,495
  • 4
  • 30
  • 85