Phase 1: Prepare
Step 1 Warm-up recall
From M6.L1: what does mode "a" do?
Private: nothing you type here is sent or stored.
Step 2 Learning goal
By the end of this lesson you can:
- use strip and split on a line (Foundation);
- convert text to numbers with int and float (Core);
- total a column from a file (Core).
Phase 2: Teach
Step 3 A familiar example
A payroll clerk reads a line that says Ann,70 and must treat 70 as a number, not as the letters seven and zero. A program must make the same conversion.
Step 4 Clear explanation
Everything read from a file is text, including digits. To calculate, convert it.
total = 0
count = 0
with open("scores.txt") as f:
for line in f:
parts = line.strip().split(",")
total = total + int(parts[1])
count = count + 1
print(total, count)
print(total / count)155 2
77.5split(",") cuts a line at each comma and gives a list. int("70") gives the number 70, and float handles decimals. Without the conversion, "70" + "85" joins text to give "7085" instead of adding.
Always count the rows you used, as in Module 4, and compare with the rows in the file. If a line is blank or has the wrong number of parts, the program stops with an IndexError or a ValueError. The next lessons deal with these cases.
Step 5 Worked example (4 marks)
The line is "Cy,60\n". Give the value of int(line.strip().split(",")[1]) + 5.
- strip removes the newline: Cy,60. (1 mark)
- split gives ["Cy", "60"]. (1 mark)
- Item 1 is "60"; int makes it 60. (1 mark)
- 60 + 5 = 65. (1 mark)
Go deeper: the professor's notes
Rows read.
Good rows.
Reported.
Check
| In | Used | Skipped |
|---|---|---|
| 10 | 8 | 2 |
At work: A sales file
A file has bad lines.
- Count the lines read.
- Report each skipped line.
What a careful practitioner decides. Do not skip rows silently.
Phase 3: Practise
Step 6 Learner practice (instant-marked)
1. "a,b,c".split(",") gives:
A list of parts.
2. int("42") gives:
Converts text to a number.
3. Adding "3" + "4" as text gives:
Text joins.
4. int("abc") gives:
Not a number.
Phase 4: Close
Step 7 Recap
- File data arrives as text.
- strip then split then int turns a line into values.
- Count the rows you used and compare with the file.
Step 8 Exit ticket (ungraded)
For the line "Dee,90", write the code that gets the score as a number.
Private: nothing you type here is sent or stored.
Step 9 Go further (Extension)
Write a program that finds the highest score in scores.txt and prints the name.
In industry: The index that lost half its value
In January 1982 the Vancouver Stock Exchange started an index at 1000. After each update the value was cut off at three decimal places instead of rounded. About 3,000 updates a day added up, and by November 1983 the index read 524.811 when the correct figure was 1098.892.
Link to this lesson. Small errors in how data is stored can add up across thousands of operations; check totals against an independent calculation.
Source: Wikipedia, Vancouver Stock Exchange, citing the Toronto Star (29 November 1983)
Common mistakes
| Mistake | Why it fails |
|---|---|
| Adding text instead of numbers | Convert with int or float. |
| Wrong column number | The first column is 0. |
| Not counting rows | Compare the count with the file. |
Limits. Fields that contain commas inside quotes need the csv module, which the next lesson introduces.