14python 判断字符串中是否含有汉字

如题所述

1. 判断字符串中是否含有汉字。

def has_hz(text):

hz_yes = False

for ch in text:

if isinstance(ch, unicode):

if unicodedata.east_asian_width(ch)!= 'Na':

hz_yes = True

break

else:

continue

return hz_yes

def has_hz(text):

hz_yes = False

for ch in text:

if isinstance(ch, unicode):

if unicodedata.east_asian_width(ch)!= 'Na':

hz_yes = True

break

else:

continue

return hz_yes

单元测试:

assert not has_hz("")

assert not has_hz(" ")

assert not has_hz("123")

assert not has_hz(u"123abc")

assert has_hz(u"123abc汉字")

assert has_hz(u"汉字")

assert not has_hz("")

assert not has_hz(" ")

assert not has_hz("123")

assert not has_hz(u"123abc")

assert has_hz(u"123abc汉字")

assert has_hz(u"汉字")
温馨提示:答案为网友推荐,仅供参考
相似回答