[Tutor] string.unprintable?

Alan Gauld alan.gauld at blueyonder.co.uk
Fri Nov 21 16:24:55 EST 2003


> but there is no string.unprintable, and
> 
> if not string.printable in line
> 
> for some reason matches everything in the file, 

Look at what the in operator does:

>>> if 'abc' in 'a big cat': print 'True'
...
>>> if 'abc' in 'abcdef': print 'True'
...
True

Thus it only returns true if the entire string is found in 
the right order in your line. The likeliehood of all the 
printable characters appearing in one of your lines in the 
exact order that python lists them is remote!

You can either use a nested loop or more usefully a regular 
expression:

import re,string

regex = "[%s]" % string.printable

printable = re.compile(regex)

if printable.search(line):
   # do it here.
else:  # or not

Caveat the above re code is untested!

HTH,

Alan G.







More information about the Tutor mailing list