Skip to content Skip to sidebar Skip to footer

Read_csv Reads \, As A Separator

I know that read_csv() uses comma (,) as separator but I have a file which some of its cells has comma in their content. In that file author used backslash comma (\,) to show that

Solution 1:

You need to configure the backslash as an escape character, with the escapechar option:

pandas.read_csv(fileobj_or_filename, escapechar='\\')

Demo:

>>>import pandas, csv>>>from io import StringIO>>>f = StringIO(r'''346882588,206801833,1049600263,Dzianis Dzenisiuk,5,StuckPixel\, Inc.,Feb 11\, 2010,2,3,1265846400...''')>>>df = pandas.read_csv(f, names='abcdefghij', escapechar='\\')>>>df['f']
0    StuckPixel, Inc.
Name: f, dtype: object
>>>df['g']
0    Feb 11, 2010
Name: g, dtype: object

Post a Comment for "Read_csv Reads \, As A Separator"