2016-08-16 54 views
1

python中的新增功能,並嘗試構建簡單的CSV閱讀器以創建現有工具的新交易。理想情況下,我想創建一個字典來簡化設置新交易所需的參數(而不是使用行[1],[2],[3]等,我想用我的標題替換閱讀價值日期,交易日期,價格,數量等)引用CSV閱讀器的Python字典中的鍵

我已經在下面創建了字典鍵,但是無法將它們鏈接到我的腳本以創建新的交易。我應該如何替換行?任何建議感激!謝謝...下面

代碼:

import acm 
import csv 

# Opening CSV file 
with open('C:\Users\Yina.Huang\Desktop\export\TradeBooking.csv', 'rb') as f: 
reader = csv.DictReader(f, delimiter=',') 
next(reader, None) 

for row in reader: 

    # Match column header with column number 
    d = { 
     row["Trade Time"], 
     row["Value Day"], 
     row["Acquire Day"], 
     row["Instrument"], 
     row["Price"], 
     row["Quantity"], 
     row["Counterparty"], 
     row["Acquirer"], 
     row["Trader"], 
     row["Currency"], 
     row["Portfolio"], 
     row["Status"] 
     } 

    NewTrade = acm.FTrade()  
    NewTrade.TradeTime = "8/11/2016 12:00:00 AM" 
    NewTrade.ValueDay = "8/13/2016" 
    NewTrade.AcquireDay = "8/13/2016" 
    NewTrade.Instrument = acm.FInstrument[row["Instrument"]] 
    NewTrade.Price = row[4] 
    NewTrade.Quantity = row[5] 
    NewTrade.Counterparty = acm.FParty[row[6]] 
    NewTrade.Acquirer = acm.FParty[row[7]] 
    NewTrade.Trader = acm.FUser[row[8]] 
    NewTrade.Currency = acm.FCurrency[row[9]] 
    NewTrade.Portfolio = acm.FPhysicalPortfolio[row[10]] 
    NewTrade.Premium = (int(row[4])*int(row[5])) 
    NewTrade.Status = row[11] 

    print NewTrade 
    NewTrade.Commit() 
+0

如何通過排數字索引和鍵引用? Row是一個字典,所以我不確定'row [4]'是如何工作的,因爲DictReader只會使用字符串字典鍵。 – Euan

+0

只要你知道,在你編輯之後,你不再創建一本字典,而是一套。 –

+0

謝謝 - 是的,它看起來像我做的。我能用字典和字典完成相同的操作嗎? –

回答

1

csv模塊已經提供了與csv.DictReader對象此功能。

with open('C:\Users\Yina.Huang\Desktop\export\TradeBooking.csv', 'rb') as f:  
    reader = csv.DictReader(f) 
    for row in reader: 

     NewTrade = acm.FTrade()  
     NewTrade.TradeTime = row['Trade Time'] 
     NewTrade.ValueDay = row['Value Day'] 
     NewTrade.AcquireDay = row['Aquire Day'] 
     NewTrade.Instrument = acm.Finstrument[row['Instrument']] 
     NewTrade.Price = row['Price'] 
     NewTrade.Quantity = row['Quantity'] 
     # etc 

documentation

Create an object which operates like a regular reader but maps the information read into a dict whose keys are given by the optional fieldnames parameter. The fieldnames parameter is a sequence whose elements are associated with the fields of the input data in order. These elements become the keys of the resulting dictionary. If the fieldnames parameter is omitted, the values in the first row of the csvfile will be used as the fieldnames. If the row read has more fields than the fieldnames sequence, the remaining data is added as a sequence keyed by the value of restkey. If the row read has fewer fields than the fieldnames sequence, the remaining keys take the value of the optional restval parameter.

+0

OP已經在使用'DictReader',但好像代碼混合了'csv.reader'和'csv。,DictReader'代碼 – Euan

+0

@Euan在編輯之前OP沒有使用'DictReader' –

+1

抱歉混淆了@ Euan--這是正確的,我修改了我的代碼以包含DictReader。我正在嘗試更新以包含相關字段並收到以下錯誤消息:KeyError:'Instrument' –