计算代码中中文字符数

def hans_count(str):
    hans_total = 0
    for s in str:
        # 基本汉字
        if '\u4e00' <= s <= '\u9fef':
            hans_total += 1
        # 基本汉字补充
        if '\u9FA6' <= s <= '\u9FFF':
            hans_total += 1
        # 扩展A
        if '\u3400' <= s <= '\u4DBF':
            hans_total += 1
        # 扩展B
        if '\u20000' <= s <= '\u2A6DF':
            hans_total += 1
        # 扩展C 扩展D
        if '\u2A700' <= s <= '\u2B81D':
            hans_total += 1
        #  扩展E
        if '\u2B820' <= s <= '\u2CEA1':
            hans_total += 1
        # 扩展F
        if '\u2CEB0' <= s <= '\u2EBE0':
            hans_total += 1
        # 扩展G
        if '\u30000' <= s <= '\u3134A':
            hans_total += 1
        # 扩展H
        if '\u31350' <= s <= '\u323AF':
            hans_total += 1
        # 康熙部首
        if '\u2F00' <= s <= '\u2FD5':
            hans_total += 1
        # 部首扩展
        if '\u2E80' <= s <= '\u2EF3':
            hans_total += 1
        # 兼容汉字
        if '\u2F800' <= s <= '\u2FA1D':
            hans_total += 1
        # 汉字笔画
        if '\u31C0' <= s <= '\u31E3':
            hans_total += 1
        # 汉字结构
        if '\u2FF0' <= s <= '\u2FFB':
            hans_total += 1
        # 汉语注音
        if '\u3105' <= s <= '\u312F':
            hans_total += 1
        # 注音扩展
        if '\u31A0' <= s <= '\u31BF':
            hans_total += 1

    return hans_total

汉字 Unicode 编码范围

posted @ 2023-03-17 15:53  白云君  阅读(24)  评论(0编辑  收藏  举报