File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11# report.py
22#
33# Exercise 2.4
4+
5+ import csv
6+
7+ def read_portfolio (filename ):
8+ """Open portfolio and read into structured data
9+
10+ :filename: name of portfolio file
11+ :returns: python list of shares and their information
12+
13+ """
14+ portfolio = []
15+
16+ with open (filename , 'rt' ) as f :
17+ rows = csv .reader (f )
18+ headers = next (rows )
19+ for row in rows :
20+ holding = {
21+ 'name' : row [0 ],
22+ 'shares' : int (row [1 ]),
23+ 'price' : float (row [2 ])
24+ }
25+
26+ portfolio .append (holding )
27+
28+ return portfolio
29+
30+ def read_prices (filename ):
31+ """Open prices file and read into structured data
32+
33+ :filename: name of prices file
34+ :returns: python list of prices for a share
35+
36+ """
37+ prices = []
38+
39+ with open (filename , 'rt' ) as f :
40+ rows = csv .reader (f )
41+ for row in rows :
42+ try :
43+ share = {
44+ 'name' : row [0 ],
45+ 'price' : float (row [1 ])
46+ }
47+ prices .append (share )
48+ except IndexError as e :
49+ print ("Error indexing incoming data" )
50+ continue
51+
52+ return prices
You can’t perform that action at this time.
0 commit comments