forked from dabeaz-course/practical-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpcost_cli.py
More file actions
30 lines (24 loc) · 795 Bytes
/
pcost_cli.py
File metadata and controls
30 lines (24 loc) · 795 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
29
30
# Manipulate the Data/portfolio.csv CSV file
# Compute the cost of all the stocks
import sys
import csv
def portfolio_cost(filename):
with open(filename, 'rt') as f:
rows = csv.reader(f)
headers = next(rows)
total_cost = 0
for lineno, stock in enumerate(rows, start=1):
try:
shares = int(stock[1])
share_cost = float(stock[2])
total_cost = total_cost + shares * share_cost
except ValueError:
print(
f"Warning: skipping invalid entry '{stock} on line {lineno}")
return total_cost
if len(sys.argv) == 2:
filename = sys.argv[1]
else:
filename = 'Data/portfolio.csv'
total_cost = portfolio_cost(filename)
print('Total cost', total_cost)