forked from dabeaz-course/practical-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpcost.py
More file actions
28 lines (25 loc) · 643 Bytes
/
pcost.py
File metadata and controls
28 lines (25 loc) · 643 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# pcost.py
#
# Exercise 1.27
import csv
import sys
def portfolio_cost(file):
'Read CSV file of stock, shares, price and compute total cost'
f = open(file, 'rt')
rows = csv.reader(f)
headers = next(rows)
total = 0
for row in rows:
try:
stock, shares, price = row
total = total + int(shares)*float(price)
except ValueError: # handle missing values
print("Couldn't parse", row)
f.close()
return total
if len(sys.argv) == 2:
filename = sys.argv[1]
else:
filename = 'Data/portfolio.csv'
total = portfolio_cost(filename)
print(f"Total cost {total:.2f}")