yuzhouyichen 发表于 2017-7-25 11:28:57

请教大家,为什么python中isalpha()函数会把汉字判定为字母?

零基础学习python19课练习题
def count(*param):
    length = len(param)
    for i in range(length):   
      letters = 0
      space = 0
      digit = 0
      other = 0
      for each in param:
            if each.isalpha():
                letters += 1
            elif each.isdigit():
                digit += 1
            elif each ==" ":
                space += 1
            else:
                other += 1
      print("第%d个字符串有:英文字母%d个,空格%d个,数字%d个,其他字符%d个"%(i+1,letters,space,digit,other))

count("汉字")

ba21 发表于 2017-7-25 12:09:11

对于unicode string,string.isalpha会根据字符串中的字符是否属于Unicode编码的LETTER区域来判断是否都由字母组成。所以得出的结果为True,不一定表示只有26个英文字母。
def count(*param):
    length = len(param)
    for i in range(length):   
      letters = 0
      space = 0
      digit = 0
      other = 0
      for each in param:
            print(each)
            if each.encode('UTF-8').isalpha():
                letters += 1
            elif each.encode('UTF-8').isdigit():
                digit += 1
            elif each ==" ":
                space += 1
            else:
                other += 1
      print("第%d个字符串有:英文字母%d个,空格%d个,数字%d个,其他字符%d个"%(i+1,letters,space,digit,other))
页: [1]
查看完整版本: 请教大家,为什么python中isalpha()函数会把汉字判定为字母?