[英]Python: Check if a string contains chinese character?
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 我发现这个答案可能有用: 使用Python和Regex查找字符串中的所有中文文本
but it didn't work out: 但它没有成功:
import re
ipath= "./data/NCDC/上海/虹桥/9705626661750dat.txt"
re.findall(ur'[\u4e00-\u9fff]+', ipath) # => []
The matched string should be unicode as well 匹配的字符串也应该是unicode
>>> 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. 如果您只想知道字符串中是否有中文字符,则不需要
re.findall
,请使用re.search
以及匹配对象的事实是真实的。
>>> 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
And for those of us who don't care for re
: 对于我们这些不关心
re
:
>>> ipath= u"./data/NCDC/上海/虹桥/6240476818161dat.txt"
>>> for i in range(len(ipath)):
... if ipath[i] > u'\u4e00' and ipath[i] < u'\u9fff':
... print ipath[i]
...
上
海
虹
桥
Edit: for the full list of Chinese characters this SO link is worth looking at as the range U+4E00..U+9FFF is not complete. 编辑:对于完整的中文字符列表,此SO链接值得查看,因为范围U + 4E00..U + 9FFF未完成。 What's the complete range for Chinese characters in Unicode?
Unicode中的汉字完整范围是什么?
import re
ipath= raw_input()
print re.findall(ur'[\u4e00-\u9fff]+', ipath.decode("utf-8"))
Output: ./data/NCDC/上海/虹桥/9705626661750dat.txt [u'\上\海', u'\虹\桥']
输出:
./data/NCDC/上海/虹桥/9705626661750dat.txt [u'\上\海', u'\虹\桥']
You need to decode the input to make it unicode. 您需要解码输入以使其成为unicode。
or 要么
import re
ipath= unicode(raw_input(),encoding="utf-8")
print re.findall(ur'[\u4e00-\u9fff]+', ipath)
''
is a bytestring on Python 2. Either add from __future__ import unicode_literals
at the top of the module or use unicode literals: u''
: ''
是Python 2上的字节from __future__ import unicode_literals
。要么在模块顶部添加from __future__ import unicode_literals
,要么使用unicode文字: u''
:
>>> import re
>>> ipath= u"./data/NCDC/上海/虹桥/9705626661750dat.txt"
>>> re.findall(ur'[\u4e00-\u9fff]+', ipath)
[u'\u4e0a\u6d77', u'\u8679\u6865']
Using these codepoint ranges , we can write an is_cjk
function: 使用这些代码点范围 ,我们可以编写一个
is_cjk
函数:
# list of cjk codepoint ranges
# tuples indicate the bottom and top of the range, inclusive
cjk_ranges = [
( 0x4E00, 0x62FF),
( 0x6300, 0x77FF),
( 0x7800, 0x8CFF),
( 0x8D00, 0x9FCC),
( 0x3400, 0x4DB5),
(0x20000, 0x215FF),
(0x21600, 0x230FF),
(0x23100, 0x245FF),
(0x24600, 0x260FF),
(0x26100, 0x275FF),
(0x27600, 0x290FF),
(0x29100, 0x2A6DF),
(0x2A700, 0x2B734),
(0x2B740, 0x2B81D),
(0x2B820, 0x2CEAF),
(0x2CEB0, 0x2EBEF),
(0x2F800, 0x2FA1F)
]
def is_cjk(char):
char = ord(char)
for bottom, top in cjk_ranges:
if char >= bottom and char <= top:
return True
return False
Which we can then use to process text, using functions like filter
, any
, all
, and map
to process the text character-by-character, or compose more complex functions: 然后我们可以使用它来处理文本,使用
filter
, any
, all
和map
等函数来逐个字符地处理文本,或者组成更复杂的函数:
txt = "./data/NCDC/上海/虹桥/9705626661750dat.txt"
txt_sanitized = "./data/NCDC/9705626661750dat.txt"
any(map(is_cjk, txt)) # True
any(map(is_cjk, txt_sanitized)) # False
''.join(filter(is_cjk, txt)) # '上海虹桥'
Note that the CJK ranges will include not only Chinese characters but also may include Korean and Japanese characters. 请注意,CJK范围不仅包括中文字符,还包括韩文和日文字符。 For more complex usage, try a dedicated library like
cjklib
. 要获得更复杂的用法,请尝试使用像
cjklib
这样的专用库。
In python 3.6 i used this 在python 3.6中我使用了这个
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
根据这个问题 ,范围应该是[\⺀-\⿕\㆐-\㆟\㐀-\䶿\一-\鿌]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.