Pandas – Is it possible to read_csv with no quotechar?
Question:
I am trying to read a csv file that has single instances of "
in certain lines, ex:
car,"plane,jet
jet,ski,"hat
When I use pandas read_csv
to read this file, it recognizes "
as a quote character and does not correctly read in lines such as the one above. I would like to not have any quote character at all when I use read_csv
.
I tried setting quotechar=None
and quotechar=''
but both spits out an error since quotechar
has to be a string of length 1. Is it possible to not have a quotechar at all when using read_csv
?
Thanks!
Answers:
From the Pandas Documentation
quoting : int or csv.QUOTE_* instance, default None
Control field quoting behavior per csv.QUOTE_* constants. Use one of QUOTE_MINIMAL (0), QUOTE_ALL (1), QUOTE_NONNUMERIC (2) or QUOTE_NONE (3). Default (None) results in QUOTE_MINIMAL behavior.
So you’ll want to include
quoting=3
as a parameter to your read_csv()
.
Jkdc’s answer is correct, but I find it more readable to actually use the csv.QUOTE* instance as mentioned in the documentation. It wasn’t clear to me which csv it meant at first, so I didn’t know how to import that. Here’s a code sample:
import pandas as pd
import csv
df1 = pd.read_csv('input_file.csv', quoting=csv.QUOTE_NONE)
I am trying to read a csv file that has single instances of "
in certain lines, ex:
car,"plane,jet
jet,ski,"hat
When I use pandas read_csv
to read this file, it recognizes "
as a quote character and does not correctly read in lines such as the one above. I would like to not have any quote character at all when I use read_csv
.
I tried setting quotechar=None
and quotechar=''
but both spits out an error since quotechar
has to be a string of length 1. Is it possible to not have a quotechar at all when using read_csv
?
Thanks!
From the Pandas Documentation
quoting : int or csv.QUOTE_* instance, default None
Control field quoting behavior per csv.QUOTE_* constants. Use one of QUOTE_MINIMAL (0), QUOTE_ALL (1), QUOTE_NONNUMERIC (2) or QUOTE_NONE (3). Default (None) results in QUOTE_MINIMAL behavior.
So you’ll want to include
quoting=3
as a parameter to your read_csv()
.
Jkdc’s answer is correct, but I find it more readable to actually use the csv.QUOTE* instance as mentioned in the documentation. It wasn’t clear to me which csv it meant at first, so I didn’t know how to import that. Here’s a code sample:
import pandas as pd
import csv
df1 = pd.read_csv('input_file.csv', quoting=csv.QUOTE_NONE)