Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: "quotechar" must be an 1-character string

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.

like image 828
Shubham Goyal Avatar asked Dec 04 '22 10:12

Shubham Goyal


1 Answers

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

  • if a CSV field contains the separator character (e. g. a comma)
  • if a CSV field contains a linefeed characters.

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.

like image 176
Tim Pietzcker Avatar answered Dec 15 '22 20:12

Tim Pietzcker