Python3 implements full-width and half-width conversion.

Source: Internet
Author: User
Tags ustring

Python3 implements full-width and half-width conversion.

Preface

This article describes how to convert the full-width and half-width characters in python3 for your reference.

I. background

What problems can be solved:Quick and convenient automatic conversion of full-width and half-width text

Applicable scenarios:Replace all-round characters with half-width characters in student answer data

Ii. Principle of full-width halfwidth

Full angle:Double Byte Character (DBC)

The half corner is:Single Byte Character, SBC for short

In windows, both Chinese characters and full-byte characters occupy two bytes, and ascii chart 2 (codes 128-255) is used );
The first byte of the fullwidth character is always set to 163, while the second byte is the same half-width attention code plus 128 (excluding spaces, the fullwidth and halfwidth spaces should also be taken into account );

For Chinese, its first byte is set to greater than 163. For example, if 'A' is 176 162, no conversion is performed when Chinese characters are detected.

For example, if the halfwidth a is 65, the fullwidth a is 163 (the first byte) and 193 (the second byte, 128 + 65 ).

Full-width halfwidth example: (the text test.txt contains full-width and halfwidth characters)

F: \ test> type test.txt 123456123456abcdefgabcdefg hello, China

3. Use Python3 to implement full-width half-width conversion

#-*-Coding: UTF-8-*-# I @mail.chenpeng.info "'full angle: Double Byte Character, referred to as: DBC half angle namely: Single Byte Character, referred to: SBC "'def DBC2SBC (ustring):" 'fullwidth to halfwidth "'rstring =" for uchar in ustring: inside_code = ord (uchar) if inside_code = 0x3000: inside_code = 0x0020 else: inside_code-= 0xfee0 if not (0x0021 <= inside_code and inside_code <= 0x7e): rstring + = uchar continue rstring + = chr (inside_code) return rst Ringdef SBC2DBC (ustring): "'halfwidth to fullwidth" 'rstring = "" for uchar in ustring: inside_code = ord (uchar) if inside_code = 0x0020: inside_code = 0x3000 else: if not (0x0021 <= inside_code and inside_code <= 0x7e): rstring + = uchar continue inside_code + = 0xfee0 rstring + = chr (inside_code) return rstrings = "'array ('0' => '0', '1' => '1', '2' => '2 ', '3' => '3', '4' => '4', '5' => '5', '6' => '6 ', '7' => '7', '8' => '8', '9' => '9', 'A' => 'A', 'B' => 'B ', 'c' => 'C', 'D' => 'D', 'E' => 'E', 'F' => 'F ', 'G' => 'G', 'H' => 'h', 'I' => 'I', 'J' => 'J ', 'K' => 'k', 'L' => 'l', 'M' => 'M', 'n' => 'n ', 'o' => 'O', 'p' => 'P', 'q' => 'Q', 'R' => 'R ', 'S '=>'s', 't' => 'T', 'U' => 'U', 'V' => 'V ', 'W' => 'w', 'x' => 'x', 'y' => 'y', 'z' => 'Z ', 'A' => 'A', 'B' => 'B', 'C' => 'C', 'D' => 'D ', 'E' => 'E ', 'F' => 'F', 'G' => 'G', 'H' => 'h', 'I' => 'I ', 'J' => 'J', 'K' => 'k', 'L' => 'l', 'M' => 'M ', 'N' => 'n', 'O' => 'O', 'p' => 'P', 'q' => 'Q ', 'R' => 'R', 's' =>'s ', 't' => 'T', 'U' => 'U ', 'V' => 'V', 'w' => 'w', 'x' => 'x', 'y' => 'y ', 'Z' => 'Z', '(' => '(', ')' => ')', '[' => '[', ']' => ']', '[' => '[', ']' => ']', '<' => '[', '〗' => ']', '"' => '[', '"' => ']', '\ "=> '[', '\ "=>'] ',' {' => '{', '}' => '}', '=>' <',' "'=> ', '%' => '%', '+' => '+', '-' => '-', '-' => '-','~ '=>'-',': '=> ':','. '=> '. ',', '=>', '=> '. ',', '=> '. ','; '=> ',','? '=> '? ','! '=> '! ','... '=>'-', ''' =>' | ',' "'=>'" ',' \ "=> ''', '\ "=> ''',' | '=>' | ', 'Region' =>'" ', ''=> ''); '# full-width to half-width print (DBC2SBC (s) # half-width to full-width print (SBC2DBC (s )) s = "'test in Chinese" '# print (DBC2SBC (s) # print (SBC2DBC (s ))

Iv. Summary

The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.

V. References

Http://thinkerou.com/2015-06/covert-dbc-sbc/

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.