Python: Check if a string contains chinese charact

2020-02-23 08:51发布

A string maybe this

ipath= "./data/NCDC/上海/虹桥/9705626661750dat.txt"

or this

ipath = './data/NCDC/ciampino/6240476818161dat.txt'

How do I know the first string contains chinese?

I find this answer maybe helpful: Find all Chinese text in a string using Python and Regex

but it didn't work out:

import re
ipath= "./data/NCDC/上海/虹桥/9705626661750dat.txt"
re.findall(ur'[\u4e00-\u9fff]+', ipath) # => []

8条回答
别忘想泡老子
2楼-- · 2020-02-23 09:19

In python 3.6 i used this

def find_china_symbols(text):
"""

:param text: input text with wrong symbols
:return: True if incorrect char exists in text
"""

for char in text:
    if ord(char) > 10000:
        print(char, ': ', ord(char))
        return True
查看更多
我命由我不由天
3楼-- · 2020-02-23 09:20

The matched string should be unicode as well

>>> import re
>>> ipath= u"./data/NCDC/上海/虹桥/9705626661750dat.txt"
>>> re.findall(r'[\u4e00-\u9fff]+', ipath)
[u'\u4e0a\u6d77', u'\u8679\u6865']
查看更多
登录 后发表回答