phillyooo phillyooo - 2 months ago 7
R Question

Importing csv file with line breaks to R or Python Pandas

I have a csv file that includes line breaks within columns:

"id","comment","x"
1,"ABC\"xyz",123
2,"xyz\"abc",543
3,"abc
xyz",483


ID 3, for example contains such a line break.

How can this be imported into python or R? Also, I don't mind if those line breaks were to be replaced by a space, for example.

Answer

Python has built-in CSV reader which handles that for you. See csv documentation.

import csv

with open(filename) as f:
    reader = csv.reader(f)
    csv_rows = list(reader)