3
$\begingroup$

I'm importing CSV files and some of the fields include quoted text with commas as part of the field, rather than as a field separator. What is the syntax (or alternative method entirely) to import and split the string by comma, while ignoring commas within quotes?

raw = ReadList[file, String];
StringSplit[raw[[#]], ","] &/@Range@Length@raw

This splits each line of the file on commas, but for lines like:

a,b,"c,c",d,e

I get:

a
b
"c
c"
d
e

whereas I would like:

a
b
"c,c"
d
e

(I have represented the elements of the example line as a column to emphasize unique fields)

$\endgroup$
  • $\begingroup$ I think if your provide your CSV files,you will get more beauty solution $\endgroup$ – yode Jun 28 '16 at 18:38
  • $\begingroup$ @yode - we can try to just experiment with the one line, StringSplit["a,b,\"c,c\",d,e", ","] to try and get it to work $\endgroup$ – Jason B. Jun 28 '16 at 18:41
  • 1
    $\begingroup$ Related: mathematica.stackexchange.com/q/56876/12 $\endgroup$ – Szabolcs Jun 28 '16 at 18:54
3
$\begingroup$

I found the problem is with my original data; there are some unexpected Line Feed characters in my data which were causing me to misinterpret the result of "Import" using type CSV. The comments by Yode and JasonB prompted me to look at the file with notepad++ so I could see the LF characters. Sorry for the confusion.

$\endgroup$
  • 2
    $\begingroup$ If your Line Feed are within quotes, there is a solution here $\endgroup$ – andre314 Jun 28 '16 at 19:01
  • $\begingroup$ I'll try to get the linefeeds removed from the original source material as they are not supposed to be there, but these references given by Andre and Szabolcs are most helpful and very informative. $\endgroup$ – DrBubbles Jun 28 '16 at 19:13

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