使用Python的csv导入QUOTE_NONNUMERIC无法按预期工作

时间:2014-03-26 20:00:31

标签: python csv double-quotes quoting

我无法弄清楚这一点,也许我因为长期看待相同的东西而失明......

我在CSV文件中有这种行:

""BIN"",""Afg"",""SONIC/SONIC JET/"",1,8.9095,""Due to the dynamic nature of the exemptions granted to many operators, the Contract Price does not reflect V.A.T. / G.S.T., Mineral Oil Taxes, Federal Excise Taxes or other taxes to which an operator may be exempt.  Please contact your salesperson or World Fuel Services if you require assistance in generating a fuel price estimate."",""N/A"",""01-NOV-2013"

我试图像这样导入:

data = csv.DictReader(open(newdatafile), delimiter=',', quoting=csv.QUOTE_NONNUMERIC)
data.fieldnames = [
    'iata', 'country', 'fbo', 'quantity', 'price', 'remarks', 'special', 'validdate'
]

for row in data:
    fuelentry = FuelPriceImport()
    fuelentry.iata = row['iata']
    fuelentry.fbo = row['fbo']
    fuelentry.min_quantity = row['quantity']
    fuelentry.net_price_liter = row['price']
    fuelentry.remarks = row['remarks']
    fuelentry.save()

当我运行这段代码时,它总是抱怨:

could not convert string to float: the Contract Price does not reflect V.A.T. / G.S.T.

显然是在双引号字符串中的逗号之后。

不应该QUOTE_NONNUMERIC完全避免这种情况,因为整个文本都在双引号内吗?

1 个答案:

答案 0 :(得分:2)

您的输入格式使用 doubled 引号,这相当于转义引号的CSV。

你必须用单引号替换加倍的引号;您可以使用包装器生成器即时执行此操作:

def undoublequotes(fobject):
    for line in fobject:
        yield line.replace('""', '"')

这确实假设列数据本身不包含加倍的引号。

演示:

>>> import csv
>>> from pprint import pprint
>>> def undoublequotes(fobject):
...     for line in fobject:
...         yield line.replace('""', '"')
... 
>>> sample = '''\
... ""BIN"",""Afg"",""SONIC/SONIC JET/"",1,8.9095,""Due to the dynamic nature of the exemptions granted to many operators, the Contract Price does not reflect V.A.T. / G.S.T., Mineral Oil Taxes, Federal Excise Taxes or other taxes to which an operator may be exempt.  Please contact your salesperson or World Fuel Services if you require assistance in generating a fuel price estimate."",""N/A"",""01-NOV-2013"
... '''
>>> reader = csv.reader(undoublequotes(sample.splitlines(True)),
...                     quoting=csv.QUOTE_NONNUMERIC)
>>> pprint(next(reader))
['BIN',
 'Afg',
 'SONIC/SONIC JET/',
 1.0,
 8.9095,
 'Due to the dynamic nature of the exemptions granted to many operators, the Contract Price does not reflect V.A.T. / G.S.T., Mineral Oil Taxes, Federal Excise Taxes or other taxes to which an operator may be exempt.  Please contact your salesperson or World Fuel Services if you require assistance in generating a fuel price estimate.',
 'N/A',
 '01-NOV-2013']