2014-03-26 511 views
2

我不知道這一點,也許我越來越瞎找長期在同一個東西...CSV導入與Python的QUOTE_NONNUMERIC工作不正常

我有這種線在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

你的報價是* *一倍,這意味着該列被看作是加引號(報價翻了一番被引用引號)。 –

+1

所以我應該嘗試從csv中刪除雙引號......從來沒有想過這可能是問題 – normic

+0

我正在測試解決方法。 –

回答

2

您的輸入格式使用加倍報價,這是引用轉義的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']