Python implements full-width half-width conversion

Source: Internet
Author: User
This article mainly introduces how to implement full-width and half-width conversion in Python. it is a practical method. if you need it, refer to the example below to explain how to implement full-width and half-width conversion in Python, I believe that it can provide some reference value for everyone's Python learning. As follows:

I. Overview of full-width half-width conversion:

Full-width unicode encoding from 65281 ~ 65374 (hexadecimal 0xFF01 ~ 0xFF5E)
The unicode encoding of halfwidth characters ranges from 33 ~ 126 (hexadecimal 0x21 ~ 0x7E)
Space is special. The full angle is 12288 (0x3000), and the half angle is 32 (0x20)
Besides spaces, the full/half-width values are sorted in unicode encoding order.
Therefore, you can use the +-method to process non-space data and separate the space.

II. Full-angle to half-angle:

The implementation code is as follows:

Def strQ2B (ustring): "converts the full width of a string to half width" rstring = "" for uchar in ustring: inside_code = ord (uchar) if inside_code = 0x3000: inside_code = 0x0020 else: inside_code-= 0xfee0 if inside_code <0x0020 or inside_code> 0x7e: # After conversion, the original character rstring + = uchar rstring + = unichr (inside_code) return rstring is returned instead of a halfwidth character.

III. halfwidth to fullwidth:

The implementation code is as follows:

Def strB2Q (ustring): "converts the half width of a string to the full width" rstring = "" for uchar in ustring: inside_code = ord (uchar) if inside_code <0x0020 or inside_code> 0x7e: # The original character rstring + = uchar if inside_code = 0x0020 is returned if it is not a halfwidth character: # except for spaces, the formula for the fullwidth and halfwidth is: halfwidth = fullwidth-0xfee0 inside_code = 0x3000 else: inside_code + = 0xfee0 rstring + = unichr (inside_code) return rstring

IV. test code:

a = strB2Q("abc12345")print ab = strQ2B(a)print b

Output:

abc12345abc12345

If you are interested, you can debug and run it. I believe there will be some gains.

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.