How to check...

Steven D'Aprano steve at REMOVETHIScyber.com.au
Sat Feb 11 20:24:06 EST 2006


On Sat, 11 Feb 2006 04:48:33 -0800, augustus.kling wrote:

> Hello,
> 
> try using regular expressions. 

"Some people, when confronted with a problem, think 'I know, I'll use
regular expressions'. Now they have two problems." -- Jamie Zawinski

The original poster asked:

"How can I  check that a string does NOT contain NON English characters?"

REs are rather overkill for something so simple, don't you think?

import string
english = string.printable  # is this what you want?
english = string.ascii_letters + string.digits  # or maybe this?
english = "abc..."  # or just manually set the characters yourself

for c in some_string:
    if c not in english:
        print "Not English!!!"
        break
else:
    print "English!"



if you want it as a function, it is even more flexible:

def all_good(s, goodchars=None):
    if goodchars is None:
        goodchars = string.printable
    for c in s:
        if c not in goodchars:
            return False
    return True



-- 
Steven.




More information about the Python-list mailing list