I have a class ETF
that has many variables. I just included three below for simplicity but there are actually close to 40:
class ETF:
def __init__(self, symbol, name, asset_class):
self.symbol = symbol
self.name = name
self.asset_class = asset_class
There is another file in my project with the following code. The two #CODE NEEDED HERE
comments are where my question pertains to.
import csv
# Open the file
data = open('db.csv')
csv_data = csv.reader(data) # csv.reader
# reformat it into a python object list of lists
data_lines = list(csv_data)
headers = data_lines[1] # Retrieving the column headers
# Find the Index positions in headers for each ETF class attribute
#CODE NEEDED HERE
# create ETF objects for each line in the file
for line in data_lines[2:]:
# CODE NEEDED HERE
# Lookup the column header based on the
I also have two spreadsheets. One spreadsheet is called db.csv
and contains the information we will be using to create ETF
objects. Each row in this csv will be it's own ETF
object. The column headers on the CSV file do do not exactly match the variable names in the ETF
class and not every column is used. For that reason, I have a second spreadsheet called column_reference.csv
which I will use to map the column names in db.csv
to the ETF
variable names.
See table below for an example of the column_reference.csv
file:
Please see the image below as an example of the db.csv
file:
What code would you use to most efficiently map the column headers and create ETF objects.