Page 114 -
P. 114
idle session
Let’s confirm that you can still process your file while splitting each line. Type the following code into IDLE’s shell:
Open the data file.
>>> data = open('sketch.txt')
>>> for each_line in data:
(role, line_spoken) = each_line.split(':') Process the data, extracting each part from
print(role, end='') each line and displaying each part on screen.
print(' said: ', end='')
print(line_spoken, end='')
Man said: Is this the right room for an argument?
Other Man said: I've told you once.
Man said: No you haven't!
Other Man said: Yes I have.
Man said: When? This all looks OK.
Other Man said: Just now.
Man said: No you didn't!
Other Man said: Yes I did!
Man said: You didn't!
Other Man said: I'm telling you, I did!
Man said: You did not!
Other Man said: Oh I'm sorry, is this a five minute argument, or the full half hour?
Man said: Ah! (taking out his wallet and paying) Just the five minutes.
Other Man said: Just the five minutes. Thank you.
Other Man said: Anyway, I did.
Man said: You most certainly did not!
Traceback (most recent call last):
File "<pyshell#10>", line 2, in <module> Whoops! There’s something seriously
(role, line_spoken) = each_line.split(':') wrong here.
ValueError: too many values to unpack
It’s a ValueError, so
that must mean there’s
something wrong with the
data in your file, right?
78 Chapter 3