# report.py
#
# Exercise 2.23

import csv

f = open('Data/portfoliodate.csv')

csv_in = csv.reader(f)		# Iterator, next() will return a tuple

headers = next(csv_in)

print(headers)            # ['name', 'date', 'time', 'shares', 'price']

# *****************************************************
# (1) Define a variable specifying the columns you want:
# *****************************************************

select = ['name', 'shares', 'price']

# *****************************************************
# (2) Locate the columns - nice technique
# *****************************************************
print(headers.index('time') )

indices = [ headers.index(colname) for colname in select ]
#           ^^^^^^^^^^^^^^^^^^^^^^
print(indices)

print("Zipped Select+Indices = ", list( zip( select, indices) ), "\n" )


# row = next(csv_in)
# print("Next row = ", row)
# record = { colname: row[index] for colname, index in zip(select, indices) }   
         # This is a dict-comprehension
# print("Next record = ", record, "\n")

# Extract only the columns 'name', 'shares', 'price'] from CSV file

portfolio = [ 
    { colname: row[index] for colname, index in zip(select, indices) } 
      for row in csv_in 
    ]
print(portfolio, "\n")
