Course 2.1Lesson 2 of 5
A line of a file can hold a record: for example name colon mark. Read each line, strip the newline, split it at the separator, and change the text of the number into a number. Keep the records in a dictionary or a list, and write the whole file again when a record changes, with a newline after each line. Real files have bad lines: an empty line, a missing part, a mark that is not a number. Catch the ValueError for each line, count the bad lines with their line numbers, and keep going, so that one bad line does not stop the whole file. Counting lines, words and different words in a file is the same loop with a dictionary.
Try the code and the simulations beside the lesson.
After this lesson you can
A file is just text, but its lines often mean something. A teacher keeps a file in which each line is a name, a colon and a mark: Asha:72. The program reads the file and gets back a dictionary.
Predict first. What is the average? Who is the best student? Then run it.
The steps are always the same. Learn them as a pattern.
for line in f:.line.strip() removes the new line..split(":") gives ["Asha", "72"]. We unpack it into name, text.int(text). Everything in a file is text. A mark must be made a number before you add it.marks[name] = ....Then the usual work: the average is 77.75, and the best is Kabir with 90.
To change a record, change the dictionary, and write the whole file again with "w". There is no way to change one line in the middle of a text file. We write the names in sorted order, one line each, with a \n. The new file holds Asha:72, Kabir:90, Meera:64 and Ravi:88, one on each line.
The same loop counts lines, words and different words.
int(text) is the step that turns the file's text into a number. Without it, the comparison with the pass mark would fail.
Real files are messy. A teacher may leave an empty line, forget a mark, type eighty, or add a stray colon.
| Line | Text | Why |
|---|---|---|
| 1 | Asha:72 | good |
| 2 | Ravi:eighty | int("eighty") is a ValueError |
| 3 | (empty) | "".split(":") gives one piece, and unpacking into two fails with a ValueError |
| 4 | Meera:64:x | three pieces: too many to unpack, a ValueError |
| 5 | Kabir:90 | good |
The loop catches the ValueError, records the line number, and goes on. The result: two good records and three bad lines (2, 3 and 4). enumerate(f, start=1) gives the numbers that a person would use. A report that says "line 2 is bad" is far more useful than a crash, or than silence.
The three-step rule for a file that comes from outside: expect trouble, keep the good, and report the bad.
"72" + "85" is "7285".strip(). The last field has a \n stuck to it, and int("72\n") works, but a name with \n does not match."a". The old records stay, and you get duplicates. Use "w".Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
"w".Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
"Asha:72".split(":") give?int(text) after you read a mark?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.