4
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?

nameless
  • 89
  • 1
  • 1
  • 4
  • 4
    possible duplicate of [How do I check if a string is unicode or ascii?](http://stackoverflow.com/questions/4987327/how-do-i-check-if-a-string-is-unicode-or-ascii) – Ja8zyjits Aug 14 '15 at 07:22
  • it can't i try but not working, just need the function to replace isUpper() or maybe with other way – nameless Aug 14 '15 at 07:31
  • did you try the code mentioned in the link that i have posted? – Ja8zyjits Aug 14 '15 at 07:36
  • yes bro, but still find error. Have other suloution ? – nameless Aug 14 '15 at 07:43
  • 1
    can you upload the code you tried with the example inputs and the output / error you got? – Ja8zyjits Aug 14 '15 at 07:47
  • example if i try to use if not x in string.ascii_letters: that is only will execute all except alphabet ascii, but i want to execute all non ascii, not only alhabet in ascii. – nameless Aug 14 '15 at 15:29
  • @Ja8zyjits this question seems to be more about the nature of the codepoints in the string rather than the object type. The question you linked is *not* a duplicate. – Mark Ransom Aug 14 '15 at 15:45
  • @MarkRansom I read the question and tried to understand what he meant, but I guess I could not really interpret it properly. But then since it was about finding the special and unicode chars i googled and found out the answer did exist and i found the link. Even in a [comment](http://stackoverflow.com/questions/32004442/how-to-check-unicode-char-in-python?noredirect=1#comment51912667_32004828) here in an answer below the op did mention he needs to check if the character entered is a unicode or not. but may be I interpreted this wrong. – Ja8zyjits Aug 17 '15 at 06:09

3 Answers3

6
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
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
0

Not Sure, whats your exact requirement is

>>> u'aあä'.encode('ascii', 'ignore')
'a'

I Found this at Python: Convert Unicode to ASCII without errors

Community
  • 1
  • 1
Romil Shah
  • 95
  • 8
-1

you can chech if a string is unicode by using the method isinstance()

s = u'aあä'
if isinstance(s, unicode):
  do_something()
6160
  • 1,002
  • 6
  • 15