Python checks whether a string contains the specified Chinese

python matches a Chinese character

UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)

 

Two methods:

#coding=UTF-8
import re

cityName="Hangzhou City, Zhejiang Province"
#name = unicode(cityName, "gbk")
#name=cityName.encode("utf-8") #When testing, utf-8 does not work, unsolved
name=cityName
print(name)
    
    
#method 1    
if name.find(u"province") !=-1: # contains 'province'
   #print u'has province'
   name=name.split(u'省')[1]
if name.find(u"city") != -1:#include 'city'
   #print u 'There is a city'
   name=name.split(u'市')[0]
    
print(name)


#Method 2
name='No results found'
if re.match(u'^\u672a\u67e5\u8be2\u5230\u7ed3\u679c', name): #code is the unicode format in which the result is not found in Chinese characters
    print('{0} has no SB!' .format(name))
else:
    print('============{0} has SB!' .format(name))    

 。。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326205809&siteId=291194637