Detec nonascii in a string

S

Sebastian Bassi

Hello,

How do I detect non-ascii letters in a string?
I want to detect the condition that a string have a letter that is not
here: string.ascii_letters

Best regards,
SB.
 
D

Diez B. Roggisch

Sebastian said:
Hello,

How do I detect non-ascii letters in a string?
I want to detect the condition that a string have a letter that is not
here: string.ascii_letters

"äöü".decode("ascii")

should do the trick -- you get an UnicodeError when there is anything ascii
can't encode.

Diez
 
S

Steven D'Aprano

Hello,

How do I detect non-ascii letters in a string?
I want to detect the condition that a string have a letter that is not
here: string.ascii_letters

for c in some_string:
if c not in string.ascii_letters:
raise ValueError("Non-ascii value!!!")

Instead of raising an error, you can take whatever action you prefer:

for c in some_string:
if c not in string.ascii_letters:
print "Character '%s' is non-ascii." % repr(c)

Or turn it into a function:

def isascii(s):
for c in some_string:
if c not in string.ascii_letters:
return False
return True
 
S

Sebastian Bassi

"äöü".decode("ascii")
should do the trick -- you get an UnicodeError when there is anything ascii
can't encode.

Thank you. This is good enought for me.
Best regards,
SB.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top