I am trying to read data from a csv file. I set quotechar to csv.QUOTE_NONE.
The four lines of Python I wrote for this purpose are just as follows -
import csv
with open('mtz.gps.comfort_gps_logs_20110214_20110215.csv', 'rb') as csvfile:
taxiDataReader = csv.reader(csvfile, delimiter = ',', quotechar = csv.QUOTE_NONE)
for row in taxiDataReader:
print row
However, when I run these, I get this error message -
Traceback (most recent call last):
File "log.py", line 3, in <module>
taxiDataReader = csv.reader(csvfile, delimiter = ',', quotechar = csv.QUOTE_NONE)
TypeError: "quotechar" must be an 1-character string
I would not only like to understand why this particular error shows up but also understand in more detail what the role of quotechar really is.
QUOTE_NONE
is meant as a value for the parameter quoting
, not for quotechar
.
The correct way would be to use
taxiDataReader = csv.reader(csvfile, delimiter=',', quoting=csv.QUOTE_NONE)
The docs state that quotechar
must always be a one-character string, its role is simply to choose which character should be used for quoting.
Quoting becomes necessary in a variety of situations, for example
In both these cases the CSV reader needs to know that these characters are meant as literal characters, not as control characters. So, if you wanted to put the values [1, "hello", "1,2,3", "hi\nthere"]
into a CSV file, it would be quite bad if the result was
1,hello,1,2,3,hi
there
wouldn't it? Therefore, these fields are quoted:
1,hello,"1,2,3","hi\nthere"
quoting
controls what will be quoted when (defaulting to QUOTE_MINIMAL
, i.e., only when quotes are absolutely necessary). If you turn off quoting entirely (QUOTE_NONE
), the value of quotechar
is of course meaningless.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With