Chr (), UNICHR () and Ord (), full half-width conversion, VALUEERROR:UNICHR () arg not in range () (Wide Python build)

Source: Internet
Author: User
Tags ord ustring

Chr (), UNICHR () and Ord ()

The CHR () function returns a corresponding character by using an integer (that is, 0~255) within range (256) as a parameter.

UNICHR () is just like it, except that the Unicode character is returned, and the parameter range of the UNICHR () added from Python 2.0 depends on how your Python is compiled. If it is configured to USC2 Unicode, then it is allowed to range (65536) or 0X0000-0XFFFF, if configured as UCS4, then this value should be range (1114112) or 0x000000-0x110000. If the supplied parameter is not within the allowable range, a ValueError exception is reported.

The Ord () function is a matching function for the Chr () function (for 8-bit ASCII strings) or the UNICHR () function (for Unicode objects), which returns the corresponding ASCII value, or Unicode value, as an argument with a character (a string of length 1).

If the given Unicode character exceeds your Python definition range, a TypeError exception is thrown: VALUEERROR:UNICHR () arg not in range () (Wide Python build)

def Is_chinese (uchar):"""determine if a Unicode is a Chinese character"""      ifUchar >= u'\u4e00'and Uchar<=u'\u9fa5':          returnTrueElse:          returnFalse def is_number (uchar):"""determine if a Unicode is a number"""      ifUchar >= u'\u0030'and Uchar<=u'\u0039':          returnTrueElse:          returnFalse def is_alphabet (uchar):"""determine if a Unicode is an English letter"""      if(Uchar >= U'\u0041'and Uchar<=u'\u005a') or (Uchar >= u'\u0061'and Uchar<=u'\u007a'):          returnTrueElse:          returnFalse def is_other (uchar):"""determine if non-Chinese, numeric, and English characters"""      ifNot (Is_chinese (Uchar) or Is_number (Uchar) or Is_alphabet (Uchar)):returnTrueElse:          returnFalse def b2q (uchar):"""half angle turn full angle"""Inside_code=Ord (Uchar)ifinside_code<0x0020or inside_code>0x7e: #不是半角字符就返回原来的字符returnUcharifinside_code==0x0020: #除了空格其他的全角半角的公式为: half width = Full angle-0xfee0Inside_code=0x3000      Else: Inside_code+=0xfee0      returnUNICHR (Inside_code) def q2b (Uchar):"""full angle turning half angle"""Inside_code=Ord (Uchar)ifinside_code==0x3000: Inside_code=0x0020      Else: Inside_code-=0xfee0      ifinside_code<0x0020or inside_code>0x7e: #转完之后不是半角字符返回原来的字符returnUcharreturnUNICHR (Inside_code) def stringq2b (ustring):"""Turn the string full-width to half- width"""      return "". Join ([Q2B (Uchar) forUcharinchustring]) def uniform (ustring):"""format string, complete full-width turn half-width, uppercase to lowercase work"""      returnstringq2b (ustring). Lower () def string2list (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</pre><br> <br> <pre></pre> <pre></pre>

Chr (), UNICHR () and Ord (), full half-width conversion, VALUEERROR:UNICHR () arg not in range () (wide Python build)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.