Computes the total cost (shares*price) of a portfolio file
(filename)
| 2 | |
| 3 | import csv |
| 4 | def portfolio_cost(filename): |
| 5 | ''' |
| 6 | Computes the total cost (shares*price) of a portfolio file |
| 7 | ''' |
| 8 | total_cost = 0.0 |
| 9 | |
| 10 | with open(filename, 'rt') as f: |
| 11 | rows = csv.reader(f) |
| 12 | headers = next(rows) |
| 13 | for row in rows: |
| 14 | try: |
| 15 | nshares = int(row[1]) |
| 16 | price = float(row[2]) |
| 17 | total_cost += nshares * price |
| 18 | # This catches errors in int() and float() conversions above |
| 19 | except ValueError: |
| 20 | print('Bad row:', row) |
| 21 | |
| 22 | return total_cost |
| 23 | |
| 24 | import sys |
| 25 | if len(sys.argv) == 2: |