[Tutor] Parsing a file

Paul Sidorsky paulsid@shaw.ca
Fri, 15 Mar 2002 14:35:29 -0700


Bob X wrote:

> Which works great. Can I change "print line" to "print kw + line" to get the keyword it found plus the line it found it on or is it harder than that?

Yes, except you'll actually need to print badword.  Printing kw will
print the whole list each time.  So you can use:

print badword + line

However, this will run the keyword and line together, so you might want
to put a space in there to make it nicer:

print badword + " " + line

You could also do this, which will include a space for you:

print badword, line

Lastly, the most common way to print multiple pieces of data is like
this:

print "%s %s" % (badword, line)

If you stick some formatting stuff in there you can even get them to
line up
nicely:

print "%-10s %-60s" % (badword, line)

The % syntax is probably overkill for this, though, and since it's not
all that easy to use unless you already know C (which is where it comes
from) you can probably live without it for now.

-- 
======================================================================
Paul Sidorsky                                          Calgary, Canada
paulsid@shaw.ca                        http://members.shaw.ca/paulsid/