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.