Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing csv file with line breaks to R or Python Pandas

Tags:

python

r

csv

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.

like image 304
phillyooo Avatar asked Sep 22 '16 21:09

phillyooo


1 Answers

You can also use python pandas library read_csv function. Make sure to specify escape char.

import pandas as pd
df = pd.read_csv('path_to_csv', sep=',', escapechar='\\')

Please note second backslash escaping first one. It has nothing to do with pandas or csv.

like image 148
Ali Faizan Avatar answered Sep 21 '22 01:09

Ali Faizan