python - TypeError: “quoting” must be an integer
我正在尝试定义一种新格式:
1 2 3 4 5 6 7 8 9 10 11 | import csv f = open("file.csv") reader = csv.reader(f) class my_dialect(csv.Dialect): lineterminator = ' ' delimiter: ';' quotechar: '"' reader = csv.reader(f, dialect=my_dialect) |
输出:
1 2 3 4 5 | TypeError Traceback (most recent call last) <ipython-input-121-0a8ad3677c01> in <module>() ----> 1 reader = csv.reader(f, dialect=my_dialect) TypeError:"quoting" must be an integer |
来自文档:
Dialect.quoting
Controls when quotes should be generated by the writer and recognised by the reader. It can take on any of the QUOTE_* constants
(see section Module Contents) and defaults to QUOTE_MINIMAL.
那是什么意思?
首先,您的代码中有语法错误与原始问题无关,因此请确保注意这些错误(赋值运算符是"=",而不是":")。如果您向类中添加了引用,它将修复您得到的错误。您的代码可能看起来像这样--
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #! /usr/bin/python import csv f = open("file.csv") reader = csv.reader(f) class my_dialect(csv.Dialect): lineterminator = ' ' delimiter= ';' quotechar= '"' quoting = csv.QUOTE_MINIMAL reader = csv.reader(f, dialect=my_dialect) |