Python import CSV short code (pandas?) delimited with ';' and ',' in entires
Pandas solution - use read_csv
with regex separator [;,]
. You need add engine='python'
, because warning:
ParserWarning: Falling back to the 'python' engine because the 'c' engine does not support regex separators (separators > 1 char and different from '\s+' are interpreted as regex); you can avoid this warning by specifying engine='python'.
import pandas as pd
import io
temp=u"""a;b;c
1;1,8
1;2,1
1;3,6
1;4,3
1;5,7
"""
#after testing replace io.StringIO(temp) to filename
df = pd.read_csv(io.StringIO(temp), sep="[;,]", engine='python')
print (df)
a b c
0 1 1 8
1 1 2 1
2 1 3 6
3 1 4 3
4 1 5 7
Pandas documentation says for parameters:
pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html
sep : str, default ‘,’
Delimiter to use. If sep is None, will try to automatically determine this.
Pandas did not parse my file delimited by ;
because default is not None
denoted for automatic but ,
. Adding sep
parameter set to ;
for pandas
fixed the issue.