Yina Huang
Yina Huang

Reputation: 13

Referencing keys in a Python dictionary for CSV reader

new to python and trying to build a simple CSV reader to create new trades off an existing instrument. Ideally, I'd like to build a dictionary to simplify the parameters required to set up a new trade (instead of using row[1], [2], [3], etc, I'd like to replace with my headers that read Value Date, Trade Date, Price, Quantity, etc.)

I've created dictionary keys below, but am having trouble linking them to my script to create the new trade. What should I put to substitute the rows? Any advice appreciated! Thanks...

Code below:

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()

Upvotes: 1

Views: 1715

Answers (1)

juanpa.arrivillaga
juanpa.arrivillaga

Reputation: 95957

The csv module already provides this functionality with the csv.DictReader object.

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

From the 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.

Upvotes: 1

Related Questions