defIs_chinese (UCHAR):"""determine if a Unicode is a Chinese character""" ifUchar >= u'\u4e00' andUchar<=u'\u9fa5': returnTrueElse: returnFalsedefIs_number (UCHAR):"""determine if a Unicode is a number""" ifUchar >= u'\u0030' andUchar<=u'\u0039': returnTrueElse: returnFalsedefIs_alphabet (UCHAR):"""determine if a Unicode is an English letter""" if(Uchar >= U'\u0041' andUchar<=u'\u005a')or(Uchar >= U'\u0061' andUchar<=u'\u007a'): returnTrueElse: returnFalsedefIs_other (UCHAR):"""determine if non-Chinese, numeric, and English characters""" if not(Is_chinese (UCHAR)orIs_number (UCHAR)orIs_alphabet (Uchar)):returnTrueElse: returnFalsedefb2q (UCHAR):"""half angle turn full angle"""Inside_code=Ord (Uchar)ifinside_code<0x0020orinside_code>0x7e:#returns the original character without a half-width character returnUcharifINSIDE_CODE==0X0020:#The formula for the other full-width half-width except the space is: half-width = full-width -0xfee0inside_code=0x3000Else: Inside_code+=0xfee0returnUNICHR (Inside_code)defq2b (UCHAR):"""full angle turning half angle"""Inside_code=Ord (Uchar)ifinside_code==0x3000: Inside_code=0x0020Else: Inside_code-=0xfee0ifinside_code<0x0020orinside_code>0x7e:#not half-width character returns the original character after the turn is finished returnUcharreturnUNICHR (Inside_code)defstringq2b (ustring):"""Turn the string full-width to half- width""" return "". Join ([Q2B (Uchar) forUcharinchustring]) defUniform (ustring):"""format string, complete full-width turn half-width, uppercase to lowercase work""" returnstringq2b (ustring). Lower ()defstring2list (ustring):"""separate the ustring according to Chinese, letters, numbers"""retlist=[] utmp=[] forUcharinchustring:ifIs_other (UCHAR):ifLen (utmp) = =0:Continue Else: Retlist.append ("". Join (utmp)) utmp=[] Else: Utmp.append (UCHAR)ifLen (utmp)! =0:retlist.append ("". Join (utmp))returnRetlist
Python: Common function encapsulation