Python pandas read_csv with custom separator

Question:

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?

Asked By: user11866342

||

Answers:

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')
Answered By: azro
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.