3

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)

DrBubbles
  • 1,391
  • 9
  • 17

1 Answers1

3

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.

DrBubbles
  • 1,391
  • 9
  • 17
  • 2
    If your Line Feed are within quotes, there is a solution here – andre314 Jun 28 '16 at 19:01
  • 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. – DrBubbles Jun 28 '16 at 19:13