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?
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')
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?
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')