@@ -52,9 +52,10 @@ An example when reading records from a file.
5252records = [] # Initial empty list
5353
5454with open (' Data/portfolio.csv' , ' rt' ) as f:
55+ next (f) # Skip header
5556 for line in f:
5657 row = line.split(' ,' )
57- records.append((row[0 ], int (row[1 ])) , float (row[2 ]))
58+ records.append((row[0 ], int (row[1 ]), float (row[2 ]) ))
5859```
5960
6061### Dicts as a Container
@@ -105,6 +106,11 @@ with open('Data/prices.csv', 'rt') as f:
105106 prices[row[0 ]] = float (row[1 ])
106107```
107108
109+ Note: If you try this on the ` Data/prices.csv ` file, you'll find that
110+ it almost works--there's a blank line at the end that causes it to
111+ crash. You'll need to figure out some way to modify the code to
112+ account for that (see Exercise 2.6).
113+
108114### Dictionary Lookups
109115
110116You can test the existence of a key.
@@ -335,7 +341,7 @@ accessed by key names instead of numeric column numbers. This is
335341often preferred because the resulting code is easier to read later.
336342
337343Viewing large dictionaries and lists can be messy. To clean up the
338- output for debugging, considering using the ` pprint ` function.
344+ output for debugging, consider using the ` pprint ` function.
339345
340346``` python
341347>> > from pprint import pprint
@@ -437,9 +443,9 @@ interactively to make sure it works:
437443### Exercise 2.7: Finding out if you can retire
438444
439445Tie all of this work together by adding a few additional statements to
440- your ` report.py ` program that compute gain/loss. These statements
446+ your ` report.py ` program that computes gain/loss. These statements
441447should take the list of stocks in Exercise 2.5 and the dictionary of
442- prices in Exercise 2.6 and computes the current value of the portfolio
448+ prices in Exercise 2.6 and compute the current value of the portfolio
443449along with the gain/loss.
444450
445451[ Contents] ( ../Contents.md ) \| [ Previous (2.1 Datatypes)] ( 01_Datatypes.md ) \| [ Next (2.3 Formatting)] ( 03_Formatting.md )
0 commit comments