Python: Check if a string contains chinese character?
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']
If you just want to know whether there is a chinese character in your string you don't need re.findall
, use re.search
and the fact that match objects are truthy.
>>> import re
>>> ipath= u'./data/NCDC/上海/虹桥/9705626661750dat.txt'
>>> ipath2 = u'./data/NCDC/ciampino/6240476818161dat.txt'
>>> for x in (ipath, ipath2):
... if re.search(u'[\u4e00-\u9fff]', x):
... print 'found chinese character in ' + x
...
found chinese character in ./data/NCDC/上海/虹桥/9705626661750dat.txt