1
$\begingroup$

I have a simple problem which I am not able to work around. I have this list

List1 = {"Id=\"1\"", "PostTypeId=\"1\"", "AcceptedAnswerId=\"2\"", \
"CreationDate=\"2015-10-29T15:56:52.933\",Id=\"2\"", "PostTypeId=\"1\"", "AcceptedAnswerId=\"3\"", \
"CreationDate=\"2015-10-29T15:57:52.900\",Id=\"3\"", "PostTypeId=\"2\"", "AcceptedAnswerId=\"5\"", \
"CreationDate=\"2015-10-29T15:59:52.533\""} 

and I would like to extract the values (i.e 1,2,2015-10-29T15:56:52.933...) to create afterwards a dataset with headings Id, PostTypeId, AcceptedAnswerId. Can someone suggest me a quick way to get to at least to have a list of values?

Thank very much in advance!

$\endgroup$
  • 2
    $\begingroup$ where does this list come from? To me it looks like it is read from somewhere and I would strongly suggest to change that part of the program so that a) splitting the strings on "," is done complete and not only partially and b) to collect rows into sublist. The current state makes working with the data unneccessarily complicated and error prone... $\endgroup$ – Albert Retey Apr 4 '18 at 10:09
2
$\begingroup$

One can use ToExpression for evaluation, splitting strings preliminary for substrings to contain only one assignment operator:

ToExpression /@ Flatten[StringSplit[#, ","] & /@ List1]

{"1", "1", "2", "2015-10-29T15:56:52.933", "2", "1", "3", "2015-10-29T15:57:52.900", "3", "2", "5", "2015-10-29T15:59:52.533"}

$\endgroup$
1
$\begingroup$

If you want each grouped, another option can be

r = StringSplit[StringJoin[List1], ","]
Flatten[StringCases[#, 
    "Id=\"" ~~ c1_ ~~ "\"PostTypeId=\"" ~~ c2_ ~~ 
      "\"AcceptedAnswerId=\"" ~~ c3_ ~~ "\"CreationDate=" ~~ 
      c4___ -> {c1, c2, c3, c4}] & /@ r, 1]

Mathematica graphics

or in MatrixForm

Mathematica graphics

$\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.