Python检查字符串中是否包含指定中文
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)
两种方法:
#coding=UTF-8 import re cityName="浙江省杭州市" #name = unicode(cityName, "gbk") #name=cityName.encode("utf-8") #测试时,utf-8不行,未解 name=cityName print(name) #方法1 if name.find(u"省") !=-1: # 包含'省' #print u'有省' name=name.split(u'省')[1] if name.find(u"市") != -1:#包含‘市’ #print u'有市' name=name.split(u'市')[0] print(name) #方法2 name='未查询到结果' if re.match(u'^\u672a\u67e5\u8be2\u5230\u7ed3\u679c', name): #code为汉字未查询到结果的unicode格式 print('{0} 没有 SB!' .format(name)) else: print('============{0} 有 SB!' .format(name))
。。
相关推荐
YENCSDN 2020-11-17
lsjweiyi 2020-11-17
houmenghu 2020-11-17
Erick 2020-11-17
HeyShHeyou 2020-11-17
以梦为马不负韶华 2020-10-20
lhtzbj 2020-11-17
夜斗不是神 2020-11-17
pythonjw 2020-11-17
dingwun 2020-11-16
lhxxhl 2020-11-16
坚持是一种品质 2020-11-16
染血白衣 2020-11-16
huavhuahua 2020-11-20
meylovezn 2020-11-20
逍遥友 2020-11-20
weiiron 2020-11-16