使用 Python 統計中文字符的數量


使用 Python 統計中文字符的數量

方法一,排除法

假設只有中英文字符:

import string

def str_count(str):
    '''找出字符串中的中英文、空格、數字、標點符號個數'''
    count_en = count_dg = count_sp = count_zh = count_pu = 0

    for s in str:
        # 英文
        if s in string.ascii_letters:
            count_en += 1
        # 數字
        elif s.isdigit():
            count_dg += 1
        # 空格
        elif s.isspace():
            count_sp += 1
        # 中文,除了英文之外,剩下的字符認為就是中文
        elif s.isalpha():
            count_zh += 1
        # 特殊字符
        else:
            count_pu += 1

    print('英文字符:', count_en)
    print('數字:', count_dg)
    print('空格:', count_sp)
    print('中文字符:', count_zh)
    print('特殊字符:', count_pu)


s = 'dfajl!大家@發!# 管道·符了3 54沙3發開fs\][dj'
str_count(s)

方法二,范圍判斷

Unicode 中,基本中文字符處在一個范圍區間,可以參考 漢字 Unicode 編碼范圍。寫成代碼就是:

def hans_count(str):
    hans_total = 0
    for s in str:
        # 中文字符其實還有很多,但幾乎都用不到,這個范圍已經足夠了
        if '\u4e00' <= s <= '\u9fef':
            hans_total += 1
    return hans_total

s = 'dfajl!大家@發!# 管道·符了3 54沙3發開fs\][dj'
print(hans_count(s))

參考資料:

  1. python統計中文字符數量
  2. 漢字 Unicode 編碼范圍


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM