forked from dabeaz-course/practical-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpcost215.py
More file actions
48 lines (34 loc) · 1.29 KB
/
pcost215.py
File metadata and controls
48 lines (34 loc) · 1.29 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# pcost.py
#
# Exercise 2.15
#
import csv
def portfolio_cost(filename, show_errors=False):
"""Calculates the total cost of a stock portfolio from a CSV file with handling for missing files."""
value = 0.0
data_list = []
with open(filename, 'rt') as f:
rows = csv.reader(f)
headers = next(rows) # take headers from first line
for linenum, line in enumerate(rows, start=2):
if not line: # Skip empty lines
if show_errors:
print(f'Line {linenum}: Empty line')
continue
types = [str, int, float]
try:
line = [func(val) for func, val in zip(types, line) ]
except ValueError as e:
if show_errors:
# pass
print(f'Line {linenum}: Bad line: {line}')
print(f'Line {linenum}: Reason: {e}')
continue
record = dict(zip(headers, line))
data_list.append(record)
# return value
return data_list
pf = portfolio_cost('.\\Data\\missing.csv')
print(pf)
cost = sum([int(i['shares']) * float(i['price']) for i in pf ])
print(f'\nTotal cost of portfolio: ${cost:0.2f}')