forked from dabeaz-course/practical-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreport.py
More file actions
52 lines (40 loc) · 1.15 KB
/
Copy pathreport.py
File metadata and controls
52 lines (40 loc) · 1.15 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
48
49
50
51
52
# report.py
#
# Exercise 2.4
import csv
def read_portfolio(filename):
"""Open portfolio and read into structured data
:filename: name of portfolio file
:returns: python list of shares and their information
"""
portfolio = []
with open(filename, 'rt') as f:
rows = csv.reader(f)
headers = next(rows)
for row in rows:
holding = {
'name': row[0],
'shares': int(row[1]),
'price': float(row[2])
}
portfolio.append(holding)
return portfolio
def read_prices(filename):
"""Open prices file and read into structured data
:filename: name of prices file
:returns: python list of prices for a share
"""
prices = []
with open(filename, 'rt') as f:
rows = csv.reader(f)
for row in rows:
try:
share = {
'name': row[0],
'price': float(row[1])
}
prices.append(share)
except IndexError as e:
print("Error indexing incoming data")
continue
return prices