close

# https://blog.csdn.net/u010099080/article/details/65981593

import string
from collections import namedtuple


def str_count(s):
    '''找出字符串中的中英文、空格、数字、标点符号个数'''

    count_en = count_dg = count_sp = count_zh = count_pu = 0
    s_len = len(s)
    for c in s:
        if c in string.ascii_letters:  # 从a-z和A-Z   #英文
            count_en += 1
        elif c.isdigit():              # 0-9
            count_dg += 1
        elif c.isspace():             # 空格
            count_sp += 1
        elif c.isalpha():            # Python isalpha() 方法检测字符串是否只由字母组成
            count_zh += 1            # 汉字
        else:
            count_pu += 1            # 标点符号
           
           
           
    total_chars = count_zh + count_en + count_sp + count_dg + count_pu
# /////////////////////////////////////  
    if total_chars == s_len:
        return namedtuple('Count', ['total', 'zh', 'en', 'space', 'digit', 'punc'])(s_len, count_zh, count_en, count_sp, count_dg, count_pu)
    else:
        print('Something is wrong!')
        return None
    return None


#s = '上面是引用了官网的介绍,意思就是说 TensorBoard 就是一个方便你理解、调试、优化 TensorFlow 程序的可视化工具,你可以可视化你的 TensorFlow graph、学习参数以及其他数据比如图像。'
s = 'AB and CD       我是人,你,,你好,嗎。'   # 15字

count = str_count(s)
print(s, end='\n\n')
print('该字符串共有 {} 个字符,其中有 {} 个汉字,{} 个英文,{} 个空格,{} 个数字,{} 个标点符号。'.format(count.total, count.zh, count.en, count.space, count.digit, count.punc))

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 ricky10116r2d2 的頭像
    ricky10116r2d2

    ricky10116r2d2的部落格

    ricky10116r2d2 發表在 痞客邦 留言(0) 人氣()