Test for a unicode string
"Martin v. Löwis"
martin at v.loewis.de
Thu Oct 25 00:10:06 EDT 2007
> I have a regular expression test in a script. When a unicode character
> get tested in the regex it gives an error:
>
> UnicodeError: ASCII decoding error: ordinal not in range(128)
>
> Question: Is there a way to test a string for unicode chars (ie. test
> if a string will throw the error cited above).
>
> Like:
> if unicode string:
> print 'string's line #'
> else:
> process the string
Sure:
try:
s.decode("ascii")
except UnicodeError:
print 'string's line #'
else:
process the string
Notice that you get a *decoding* error, so you are not testing
Unicode characters, but byte strings.
HTH,
Martin
More information about the Python-list
mailing list