[Tutor] Letters & Digits

dman dsh8290@rit.edu
Fri, 16 Nov 2001 17:44:07 -0500


On Fri, Nov 16, 2001 at 05:00:56PM -0500, Lee-Shanok, Bruce wrote:
| I'm currently doing some manual parsing of a string in Python, and I need to
| find a way to make sure it consists only of a particular group of
| characters. 
| 
| I may also later need to ensure that it does not include any of a number of
| characters. 

Learn regular expressions if you don't know them already.

| I know there are constants in Python that define all the digits, letters,
| printable characters, etc. What I'm wondering is if there's a clean and easy
| way to test each character in a string to see if they match (or do not
| match) characters from those lists. So far the best I've been able to come
| up with are sequences of for loops, but I'm imagining there must be a better
| way.

In the string module there are the attributes :

    digits
    uppercase
    lowercase
    whitespace
    punctuation
    printable


You probably want something like :

import re , string
# this is a regex that matches any single character that is not
# printable
nonprintable_ch = re.compile( "[^" + string.printable + "]" )
# this regex matches any sequence of printable characters,
# you can use this if you want to get the "words" out of a string
#   containing non-printable characters
printable_word = re.compile( "[" + string.printable + "]+" )
def contains_only_printable( s ) :
    return nonprintable_re.match( s ) is None

print contains_only_printable( "^B" )
print contains_only_printable( "a" )

$ python2.1 test.py 
0
1


(I created the test file using vim and entered that non-printable
character using the sequence '^Vx02' where '^V' means Ctrl-V)

HTH,
-D