Reputation: 89
def order_check_uni(body):
ccnt=0
for x in body:
if x.isUpper():
ccnt+=1
if ccnt>2:
print 'success'
I try to find char non ASCII or special char or unicode char or cyrillic char like абвгдеёжзийклмнопрстуфхцчшщъыьэюя ®©™
in string body with that script, i try to replace isUpper()
with isascii()
and len(x) == len(x.encode)
, with unichr()
and the other function but still find error, can somebody help me?
Upvotes: 4
Views: 14370
Reputation: 308091
for x in body:
if ord(x) > 127:
# character is *not* ASCII
This works if you have a Unicode string. If you just want to detect if the string contains a non-ASCII character it also works on a UTF-8 encoded byte string.
Update for Python 3: the above still works on Unicode strings, but ord
no longer works for byte strings. But that's OK, because indexing into a byte string already returns an integer - no conversion necessary! The code becomes even simpler, especially if you combine it with the any
function:
if any(x > 127 for x in body):
# string is *not* ASCII
Upvotes: 8
Reputation: 1002
you can chech if a string is unicode by using the method isinstance()
s = u'aあä'
if isinstance(s, unicode):
do_something()
Upvotes: -1
Reputation: 105
Not Sure, whats your exact requirement is
>>> u'aあä'.encode('ascii', 'ignore')
'a'
I Found this at Python: Convert Unicode to ASCII without errors
Upvotes: 1