user11866342
user11866342

Reputation:

Python pandas read_csv with custom separator

I have a CSV file where columns are separated using a non-standard symbol (||/).

df = pd.read_csv('data_analyst_assignment.csv',sep='||/', engine='python')

This throws an error:

ParserError: Expected 61 fields in line 3, saw 68. Error could possibly be due to quotes being ignored when a multi-char delimiter is used.

Can you please help me how to read this file?

Upvotes: 1

Views: 3105

Answers (1)

azro
azro

Reputation: 54148

From .read_csv()

sep:str, default ‘,’ : Delimiter to use. ... In addition, separators longer than 1 character and different from '\s+' will be interpreted as regular expressions and will also force the use of the Python parsing engine.

And | is special char in regex grammar (means OR) so you need to escape it, so you need

df = pd.read_csv('data_analyst_assignment.csv',sep='\|\|/', engine='python')

Upvotes: 4

Related Questions