I want to split a CSV line into its separate fields, however in some of the fields there are commas or new line characters.
If I use line.split(',') it picks up the commas within the quotes, and if I use the CSV class it gives me an illegal format error because of the new lines.
I want to split a CSV line into its separate fields, however in some
Share
I’m sure self-implementing it is reinventing the wheel. If stdlib’s CSV class doesn’t satisfy you try another implementation FasterCSV.
Make sure if your input format is all right: new lines and commas and escaped quotes within quotes only.
Update: According to Generating fields containing newline with Ruby CSV::Writer stdlib’s CSV has problems with fields containing newlines. I suppose it first splits rows blindly using newline as separator and not taking any escapes into account.