Head first python

Page 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: >>> data = open('sketch.txt')

Open the data file.

>>> for each_line in data: (role, line_spoken) = each_line.split(':') print(role, end='') print(' said: ', end='')

each part from Process the data, extractinhg par t on screen. each line and displaying eac

print(line_spoken, end='')

Man said:

Is this the right room for an argument?

Other Man said: Man said:

No you haven't!

Other Man said: Man said:

Yes I did! I'm telling you, I did!

You did not!

Other Man said: Man said:

This all looks OK.

You didn't!

Other Man said: Man said:

Just now.

No you didn't!

Other Man said: Man said:

Yes I have.

When?

Other Man said: Man said:

I've told you once.

Oh I'm sorry, is this a five minute argument, or the full half hour?

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> (role, line_spoken) = each_line.split(':')

sly

Whoops! There’s something seriou 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


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.