I have a mal-formatted .csv file which is caused by some extra \n. e.g.:
Name,Comment
"Peter","Good morning"
"Paul","How are you
"
"Mary","Fine"
The 2nd row ends with a unwanted, extra \n.
How can I remove all tailing \ns which are not followed by a double-quote " (assume the whole file is read into a string already)?
Don’t read the whole thing into a string, use the standard CSV parser in 1.9 to read it. If you have that in, say,
pancakes.csv, then:Then you’ll have this in
data:So you can get your data all clean and nicely parsed quite simply. And if you just need to clean up the CSV for some other program that can’t handled embedded newlines, then you can use CSV to write it back out again.