TypeError: "quotechar" must be a 1 character string

I am trying to read data from a csv file. I set quotechar to csv.QUOTE_NONE.

The four Python lines that I wrote for this purpose are 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 them, 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 just like to understand why this particular error occurs, but also to understand in more detail what is the role of Katchar.

+4
source share
1 answer

QUOTE_NONE means the value for the quoting parameter, not for quotechar .

The correct way is to use

 taxiDataReader = csv.reader(csvfile, delimiter=',', quoting=csv.QUOTE_NONE) 

The state of the docs that quotechar should always be a single-character string, its role is simply to choose which character to use to quote.

Citation becomes necessary in various situations, for example

  • if the CSV field contains a separator character (for example, a comma)
  • if the CSV field contains string characters.

In both cases, the CSV reader should be aware that these characters are denoted by literal characters, and not as control characters. So, if you want to put the values [1, "hello", "1,2,3", "hi\nthere"] in a CSV file, it would be nice if the result were

 1,hello,1,2,3,hi there 

is not it? Therefore, these fields are cited:

 1,hello,"1,2,3","hi\nthere" 

quoting controls what will be indicated when (by default QUOTE_MINIMAL used, i.e. only when quotes are absolutely necessary). If you completely disable quoting ( QUOTE_NONE ), the quotechar value quotechar , of course, pointless.

+10
source

Source: https://habr.com/ru/post/1483544/


All Articles