首页 文章

如何在python中检查Unicode char

提问于
浏览
-1
def order_check_uni(body):  
    ccnt=0  
    for x in body:    
        if x.isUpper():  
            ccnt+=1  
        if ccnt>2:  
            print 'success'

我尝试使用该脚本在字符串体中查找字符非ASCII或特殊字符或unicode字符或西里尔字母 абвгдеёжзийклмнопрстуфхцчшщъыьэюя ®©™ ,我尝试将 isUpper() 替换为 isascii()len(x) == len(x.encode) ,用 unichr() 和其他函数但仍然发现错误,有人可以帮我吗?

3 回答

  • 0
    for x in body:
        if ord(x) > 127:
            # character is *not* ASCII
    

    如果您有Unicode字符串,则此方法有效 . 如果您只想检测字符串是否包含非ASCII字符,它也适用于UTF-8编码的字节字符串 .

  • 0

    不确定,你的确切要求是什么

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

    我在Python: Convert Unicode to ASCII without errors找到了这个

  • 1

    你可以通过使用方法 isinstance() 来判断一个字符串是unicode

    s = u'aあä'
    if isinstance(s, unicode):
      do_something()
    

相关问题