V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Livid
V2EX  ›  Python

如何用 Python 判断一个字符串里出现了多少个汉字

  •  
  •   Livid · 2018-03-29 14:47:26 +08:00 · 6727 次点击
    这是一个创建于 2191 天前的主题,其中的信息可能已经有所发展或是发生改变。
    请教一种最简洁的实现方式,谢谢各位。
    16 条回复    2018-03-31 12:23:22 +08:00
    lululau
        1
    lululau  
       2018-03-29 14:53:27 +08:00   ❤️ 1
    len(re.findall(ur'[\u4e00-\u9fff]', sample)

    不过我感觉在文本处理的技术层面,所谓“汉字”应该是没有准确定义的
    SuperMild
        2
    SuperMild  
       2018-03-29 14:54:06 +08:00
    如果要区分日语、俄语等各国语言就比较麻烦了
    whoami9894
        3
    whoami9894  
       2018-03-29 14:54:48 +08:00 via Android   ❤️ 2
    正则[\u4e00-\u9fa5]+
    b821025551b
        4
    b821025551b  
       2018-03-29 14:58:02 +08:00
    上面提供的 unicode 编码范围应该不完全,比如“㗊”这个字,它是不是汉字?
    muziki
        5
    muziki  
       2018-03-29 15:00:01 +08:00 via iPhone
    import jieba
    larsenlouis
        6
    larsenlouis  
       2018-03-29 15:23:12 +08:00
    fork
        7
    fork  
       2018-03-29 15:51:57 +08:00
    @SuperMild 俄语里也会有汉字?
    huhujin
        8
    huhujin  
       2018-03-29 15:56:10 +08:00
    ```

    class Char(object):
    widths = [
    (126, 1), (159, 0), (687, 1), (710, 0), (711, 1),
    (727, 0), (733, 1), (879, 0), (1154, 1), (1161, 0),
    (4347, 1), (4447, 2), (7467, 1), (7521, 0), (8369, 1),
    (8426, 0), (9000, 1), (9002, 2), (11021, 1), (12350, 2),
    (12351, 1), (12438, 2), (12442, 0), (19893, 2), (19967, 1),
    (55203, 2), (63743, 1), (64106, 2), (65039, 1), (65059, 0),
    (65131, 2), (65279, 1), (65376, 2), (65500, 1), (65510, 2),
    (120831, 1), (262141, 2), (1114109, 1),
    ]

    @classmethod
    def get_char_width(cls, o):
    o = ord(o)
    if o == 0xe or o == 0xf:
    return 0
    for num, wid in cls.widths:
    if o <= num:
    return wid
    return 1

    @classmethod
    def get_string_width(cls, string):
    # 显示文字的宽度
    return sum(map(cls.get_char_width, string))

    ```
    hicdn
        9
    hicdn  
       2018-03-29 16:08:03 +08:00
    简单除暴实现,假设文本只包含 ASCII 字符和中文

    In [50]: bs
    Out[50]: b'>\xe5\xa6\x82\xe4\xbd\x95\xe7\x94\xa8 Python \xe5\x88\xa4\xe6\x96\xad\xe4\xb8\x80\xe4\xb8\xaa\xe5\xad\x97\xe7\xac\xa6\xe4\xb8\xb2\xe9\x87\x8c\xe5\x87\xba\xe7\x8e\xb0\xe4\xba\x86\xe5\xa4\x9a\xe5\xb0\x91\xe4\xb8\xaa\xe6\xb1\x89\xe5\xad\x97 - V2EX'

    In [51]: s = bs.decode()

    In [52]: s
    Out[52]: '>如何用 Python 判断一个字符串里出现了多少个汉字 - V2EX'

    In [53]: [i for i in s if ord(i) not in range(0x20, 0x7f)]
    Out[53]:
    ['如',
    '何',
    '用',
    '判',
    '断',
    '一',
    '个',
    '字',
    '符',
    '串',
    '里',
    '出',
    '现',
    '了',
    '多',
    '少',
    '个',
    '汉',
    '字']
    hicdn
        10
    hicdn  
       2018-03-29 16:12:36 +08:00
    接上条,\r 和 \n 也算进去了,用 0x00 替换 0x20
    qsnow6
        11
    qsnow6  
       2018-03-29 16:25:30 +08:00
    用正则去搜索 unicode 编码表应该是最优方案了
    oncethink
        12
    oncethink  
       2018-03-29 16:44:16 +08:00
    !pip install zhon
    import re
    import zhon
    len(re.findall('[{}]'.format(zhon.hanzi.characters),"中文"))
    xpresslink
        13
    xpresslink  
       2018-03-29 23:12:47 +08:00
    2E80 ~ 33FFh:中日韩符号区。收容康熙字典部首、中日韩辅助部首、注音符号、日本假名、韩文音符,中日韩的符号、标点、带圈或带括符文数字、月份,以及日本的假名组合、单位、年号、月份、日期、时间等。
    3400 ~ 4DFFh:中日韩认同表意文字扩充 A 区,总计收容 6,582 个中日韩汉字。
    4E00 ~ 9FFFh:中日韩认同表意文字区,总计收容 20,902 个中日韩汉字。
    A000 ~ A4FFh:彝族文字区,收容中国南方彝族文字和字根。
    AC00 ~ D7FFh:韩文拼音组合字区,收容以韩文音符拼成的文字。
    F900 ~ FAFFh:中日韩兼容表意文字区,总计收容 302 个中日韩汉字。
    FB00 ~ FFFDh:文字表现形式区,收容组合拉丁文字、希伯来文、阿拉伯文、中日韩直式标点、小符号、半角符号、全角符号等。
    fanhaipeng0403
        14
    fanhaipeng0403  
       2018-03-30 10:21:02 +08:00
    jieba
    robinlovemaggie
        15
    robinlovemaggie  
       2018-03-30 11:39:41 +08:00
    面试算法题既视感。。。逃~~
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4969 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 01:13 · PVG 09:13 · LAX 18:13 · JFK 21:13
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.