[Tutor] string.unprintable?

Neil Schemenauer nas-pytut at python.ca
Fri Nov 21 13:16:22 EST 2003

On Fri, Nov 21, 2003 at 11:03:54AM -0500, Clay Shirky wrote:
> I want to loop over a file with some printable and some binary lines,
> printing the former.
> What I really want is
> for line in file("spam"):
>     if string.unprintable in line:
>         continue
>     print line

No, you don't.  "string.unprintable in somestring" is a substring

> but there is no string.unprintable, and
> if not string.printable in line
> for some reason matches everything in the file, even though I've .strip()ed
> the line aready.

A regex is probably the way to go here.  Something like:

  find_unprintable = re.compile(r'[\x00-\x08\x0d-\x1f\x7f-\xff]').search
  for line in file("spam"):
      if not find_unprintable(line):
          print line
Too bad there isn't a re character class.  I guess "unprintable" may
mean different things to different people.  HTH,


More information about the Tutor mailing list