1
$\begingroup$

I imported a CSV file that uses commas as its delimiter, but some fields also contain commas. When I import the file into Mathematica as follows

file = Import["file.csv", "Table", FieldSeparators -> ","]

It works and outputs stuff like this

{{"Epitope ID", "Sequence", "Mapped Start Position", 
  "Mapped End Position", "Identity", "Subjects Tested", 
  "Subjects Responded", "Assays Positive", "Assays Negative", 
  "Response Freq.", "Lower Bound of 95% CI", 
  "Upper Bound of 95% CI"}, {150975, "DTLCIGYHANNSTDT", 18, 32, 
  "93.33%", 11, 5, 1, 0, 0.45, 0.2, 0.72}, {35073, 
  "LCLGHHAVPNGTLVKTITNDQIEVTNATELVQSSSTGKI", 20, 58, "30.77%", 1, 1, 
  1, 0, 1., 0.04, 1.}, {6570, 
  "CLGHHAVPNGTLVKTITNDQIEVTNATELVQSSSTGKIC", 21, 59, "33.33%", 1, 1, 
  1, 0, 1., 0.04, 1.}, {191055, "H25, H45, I361, D362", 25, 362, 
  "100%", 1, 1, 1, 0, 1., 0.04, 1.}, {434783, 
  "H25, H45, S46, V47, T332, V361, D362, G363, W364, Q381, K382, \
T384, Q385, I388, N389, V395, N396, I399", 25, 399, "94.44%", 1, 1, 1,
   0, 1., 0.04, 1.}, {434784, 
  "H25, H45, V47, N48, L49, T332, D362, G363, W364, Q381, T384, Q385, \
I388, N389, T392, V395, N396, I399, E400", 25, 400, "100%", 1, 1, 1, 
  0, 1., 0.04, 1.}, {151039, "NSTDTVDTVLEKNVT", 28, 42, "100%", 11, 4,
   1, 0, 0.36, 0.13, 0.64}, {417980, "D31, S46", 31, 46, "100%", 1, 1,
   1, 0, 1., 0.04, 1.}, {580001, 
  "D31, H45, S46, V47, T305, S306, T333, V362, D363, W365, L382, \
Q386, V396, N397", 31, 396, "78.57%", 1, 1, 1, 0, 1., 0.04, 1.},...}

If I put it in a TableView[file] it displays the data correctly. If I extract column 1, elements are formatted correctly in the resulting list. But if I extract column 2, which contains commas, it breaks each row into multiple elements.

How can I get Mathematica to identify each row as it is shown in the table? Those rows with commas would be sublists.

$\endgroup$
2
$\begingroup$

By default, quotation marks are shown around strings in input cells, but not in output cells. *

Since the quotation marks are not displayed, elements look as if they are broken:

file[[All, 2]]

enter image description here

To see that they are not broken, you can use

InputForm @ file[[All, 2]]

enter image description here

Update: If you need to transform strings containing commas into lists, you can use StringSplit:

StringSplit[file[[All, 2]], ","] /. {s_String} :> s

{"Sequence", "DTLCIGYHANNSTDT", "LCLGHHAVPNGTLVKTITNDQIEVTNATELVQSSSTGKI", "CLGHHAVPNGTLVKTITNDQIEVTNATELVQSSSTGKIC", {"H25", " H45", " I361", " D362"}, {"H25", " H45", " S46", " V47", " T332", " V361", " D362", " G363", " W364", " Q381", " K382", " T384", " Q385", " I388", " N389", " V395", " N396", " I399"}, {"H25", " H45", " V47", " N48", " L49", " T332", " D362", " G363", " W364", " Q381", " T384", " Q385", " I388", " N389", " T392", " V395", " N396", " I399", " E400"}, "NSTDTVDTVLEKNVT", {"D31", " S46"}, {"D31", " H45", " S46", " V47", " T305", " S306", " T333", " V362", " D363", " W365", " L382", " Q386", " V396", " N397"}}

$\endgroup$
  • $\begingroup$ I'm still looking at the docs for how to work with this -- I can't seem to access an InputForm object like a list, so how could I work with this? $\endgroup$ – briennakh Oct 5 '18 at 22:49
  • 1
    $\begingroup$ @briennakh, please see the update. $\endgroup$ – kglr Oct 5 '18 at 22:58
  • $\begingroup$ Thank you, you're amazing!! @kglr $\endgroup$ – briennakh Oct 5 '18 at 23:02

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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