开发者

Mathematica - Separate elements of a string into columns?

I'm producing encoded output that is a list of strings that look like lists. Is there a way to turn them into actual lists so I can select elements and format them into columns? Or a way to select characters in the strings and put those in columns?

  Button[
    "Run",
    {afit = Input["Please Enter ID#", deleteme] ;
    ClearAll[dat],
    dat := TraumaEncoding@afit;
    ClearAll[funcel],
    funcel[p_String] :=
     Which[StringMatchQ[
      p, ("*no new ones*" | "*no new marks*" | 
      "*old wounds healing*"), 
      IgnoreCase -> True], "0",
      StringMatchQ[p, ("*abrasion*"), IgnoreCase -> True], "{1,0}",
      StringMatchQ[p, ("*lacer*"), IgnoreCase -> True], "{1,1}",
      StringMatchQ[p, ("*punct*") | ("*pct*"), 
      IgnoreCase -> True], "{1,2}",
      StringMatchQ[p, ("*amput*"), IgnoreCase -> True], "{1,4}",
      StringMatchQ[
       p, ("*wound*" | "*injur*" | "*trauma*" | 
       "*swollen*" | "*swell*"), IgnoreCase -> True], 1, True, 0];
    fun开发者_如何学JAVAcel[___] := 0;
    ClearAll[func],
    func[l_List] := 
     Which[MemberQ[Map[funcel, l], "0"], "0", 
      MemberQ[Map[funcel, l], "{1,4}"], "{1,4}", 
      MemberQ[Map[funcel, l], "{1,2}"], "{1,2}", 
      MemberQ[Map[funcel, l], "{1,1}"], "{1,1}", 
      MemberQ[Map[funcel, l], "1-0"], "1,0", MemberQ[Map[funcel, l], 1],
       1, True, 0];
    listo := Map[func, dat]},
  Background -> Yellow, Sequence @@ $ButtonOptions
  ]

Produces something like this:

   {"{1,2}", "{1,2}", "0", "{1,2}", "0", "0", "0", "0", "0"}

I need to turn that into something more like this, ideally that I can export into an Excel file:

    Header  Header
      1       2
      1       2
      0
      1       2
      0
     etc.,


One minor note: you actually don't need to Map ToExpression to the list, instead, you can Apply it

In[91]:= ToExpression@{"{1,2}","{1,2}","0","{1,2}","0","0","0","0","0"}
Out[91]= {{1,2},{1,2},0,{1,2},0,0,0,0,0}


ToExpression["{1,2}"]

==> {1,2}

or the whole list:

data =
 Prepend[ToExpression /@ {"{1,2}", "{1,2}", "0", "{1,2}", "0", "0", 
    "0", "0", "0"}, {"header1", "header2"}]

(* ==> {{"header1", "header2"}, {1, 2}, {1, 2}, 0, {1 2}, 0, 0, 0, 0, 0}   *)

Formatted as a table:

Mathematica - Separate elements of a string into columns?

To export:

Export["filename.csv", data]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜