Python Pandas Read_csv Quotechar Does Not Work
I've read this, this and this posts but despite I don't know why quotechar does not work at pd.read_csv() (Python 3, pandas 0.18.0 and 0.18.1). And how could I read a dataframe lik
Solution 1:
Pandas doc on separators in read_csv()
:
Separators longer than 1 character and different from '\s+' will be interpreted as regular expressions, will force use of the python parsing engine and will ignore quotes in the data.
Try using this instead (sep
by default set to a comma):
pd.read_csv(file, skipinitialspace = True, quotechar = '"')
Solution 2:
Another solution is to use a proper regular expression instead of the simple \s+
. We need to find comma (,
) which is not within quotation marks:
pd.read_csv(file,
sep=', (?=(?:"[^"]*?(?: [^"]*)*))|, (?=[^",]+(?:,|$))',
engine='python')
The expression is taken from here.
Post a Comment for "Python Pandas Read_csv Quotechar Does Not Work"