When working with text data, we may encounter situations where we need to convert half-width characters to full-width characters. Full-width characters are usually used in East Asian characters such as Chinese and Japanese, while half-width characters are closer to the width of English characters. This article will introduce how to use Python to implement the conversion of half-width characters to full-width characters.
The difference between full and half
The main difference between full-width characters and half-width characters is that they occupy different widths of space. In a Unicode character set, full-width characters usually occupy two bytes of space, while half-width characters occupy only one byte of space. For example, the English letter "A" of a half-width is represented as "\u0041" in Unicode, while the corresponding full-width character is "A", and its Unicode is represented as "\uFF21".
The idea of changing
To convert half-width characters into full-width characters, we need to understand the correspondence between full-width characters and half-width characters. Generally, the correspondence between full-width characters and half-width characters can be determined by offset. Taking English letters as an example, the offset between full-width characters and half-width characters is 65248 (i.e. ord(‘A’) - ord(‘A’) = 65248). Therefore, we can get the Unicode code for the corresponding full-width character by adding 65248 to the Unicode code of the half-width character.
Convert multiple codes
Here are several ways to use Python to achieve half-width turn full-width:
Method 1: Use a simple alternative method
def half_to_full(text): full = '' for char in text: if 33 <= ord(char) <= 126: full += chr(ord(char) + 65248) else: full += char return full
Method 2: Use map function and lambda expression
def half_to_full(text): return ''.join(map(lambda c: chr(ord(c) + 65248) if 33 <= ord(c) <= 126 else c, text))
Method 3: Use regular expressions to replace
import re def half_to_full(text): return (r'[\x21-\x7e]', lambda x: chr(ord((0)) + 65248), text)
Specific use cases
text = "Hello, World! 123" full_text = half_to_full(text) print("Half-width character:", text) print("Convert to full-width characters:", full_text)
Output result:
Half-width characters: Hello, World! 123
Convert to full-width characters: Hello, World! 123
Summarize
When processing text data, the conversion of half-width characters to full-width characters is a common requirement. Using Python, we can implement the conversion of half-width characters to full-width characters through a variety of methods, including simple replacement methods, using map functions and lambda expressions, and regular expression replacement. The choice of the right method depends on the specific application scenario and personal preferences. Through the methods introduced in this article, you can easily implement the conversion of half-width characters to full-width characters to meet different text processing needs.
Replenish:
All full-width turns half a corner, which is exactly the opposite of the previous one. The formula corresponds to: full-width = half-width + 0xfee0
def Q2B(uchar): """Single Character Full-width Turns Half-width""" inside_code = ord(uchar) if inside_code == 0x3000: inside_code = 0x0020 else: inside_code -= 0xfee0 if inside_code < 0x0020 or inside_code > 0x7e: #After transferring, it is not a half-width character to return to the original character return uchar return chr(inside_code)
Turn the entire string in half a corner, or just turn parts such as numbers and letters
def stringQ2B(ustring): """Turn the full-width string in half""" return "".join([Q2B(uchar) for uchar in ustring]) def stringpartQ2B(ustring): """Turn the full-width number and letter in the string in half""" return "".join([Q2B(uchar) if is_Qnumber(uchar) or is_Qalphabet(uchar) else uchar for uchar in ustring])
This is the end of this article about the example of Python's method of implementing half-angle and full-angle. For more related Python's half-angle and full-angle content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!