python - 为什么 unicode 的中文字符串,调用 isalpha()或 isalnum()返回的是 True 呢?
PHP中文网
PHP中文网 2017-04-17 13:36:10
0
1
1468

例如:

s = u"中国"
print s.isalpha()

输出竟然为True. 这是什么原因?
那么想判断字符串是否只有字母组成出了正则,还有什么好方法?

PHP中文网
PHP中文网

认证高级PHP讲师

reply all(1)
洪涛

For unicode string, string.isalpha will determine whether the characters in the string are all composed of letters based on whether they belong to the LETTER area of ​​Unicode encoding. So the result is True, which does not necessarily mean that there are only 26 English letters.

Regular expressions should be the simplest method.

Of course you can:

def isAlpha(word):
    try:
        return word.encode('ascii').isalpha()
    except UnicodeEncodeError:
        return False

s = u"中国"
isAlpha(s)
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template