python3實(shí)現(xiàn)全角和半角字符轉(zhuǎn)換的方法示例
前言
本文主要給大家介紹了關(guān)于python3中全角和半角字符轉(zhuǎn)換的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),下面話不多說了,來一起看看詳細(xì)的介紹吧。
一、背景介紹
解決什么問題:快速方便的對(duì)文本進(jìn)行全角半角自動(dòng)轉(zhuǎn)換
適用什么場景:學(xué)生答題數(shù)據(jù)中全角字符替換為半角字符
二、全角半角原理
全角即:Double Byte Character,簡稱DBC
半角即:Single Byte Character,簡稱SBC
在 windows 中,中文和全角字符都占兩個(gè)字節(jié),并且使用了 ascii chart 2 (codes 128–255);
全角字符的第一個(gè)字節(jié)總是被置為 163,而第二個(gè)字節(jié)則是相同半角字符碼加上128(不包括空格,全角空格和半角空格也要考慮進(jìn)去);
對(duì)于中文來說,它的第一個(gè)字節(jié)被置為大于163,如'阿'為:176 162,檢測到中文時(shí)不進(jìn)行轉(zhuǎn)換。
例如:半角 a 為 65,則全角 a 是 163(第一個(gè)字節(jié))、193(第二個(gè)字節(jié),128+65)。
全角半角示例:(文本 test.txt 包含全角和半角字符)
F:\test>type test.txt 123456 123456 abcdefg abcdefg 中國你好
三、使用 Python3 實(shí)現(xiàn)全角半角轉(zhuǎn)換
# -*- coding:utf-8 -*-
# i@mail.chenpeng.info
”'
全角即:Double Byte Character,簡稱:DBC
半角即:Single Byte Character,簡稱:SBC
”'
def DBC2SBC(ustring):
”' 全角轉(zhuǎn)半角 ”'
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 rstring
def SBC2DBC(ustring):
”' 半角轉(zhuǎn)全角 ”'
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 rstring
s = ”'
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(chǎn)', ‘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',
‘(' => ‘(‘, ‘)' => ‘)', ‘〔' => ‘[‘, ‘〕' => ‘]', ‘【' => ‘[‘,
‘】' => ‘]', ‘〖' => ‘[‘, ‘〗' => ‘]', ‘”‘ => ‘[‘, ‘”‘ => ‘]',
‘\” => ‘[‘, ‘\” => ‘]', ‘{' => ‘{‘, ‘}' => ‘}', ‘《' => ‘<‘,
‘》' => ‘>',
‘%' => ‘%', ‘+' => ‘+', ‘—' => ‘-‘, ‘-' => ‘-‘, ‘~' => ‘-‘,
‘:' => ‘:', ‘。' => ‘.', ‘、' => ‘,', ‘,' => ‘.', ‘、' => ‘.',
‘;' => ‘,', ‘?' => ‘?', ‘!' => ‘!', ‘…' => ‘-‘, ‘‖' => ‘|',
‘”‘ => ‘”‘, ‘\” => ‘`', ‘\” => ‘`', ‘|' => ‘|', ‘〃' => ‘”‘,
‘ ' => ‘ ‘);
”'
# 全角轉(zhuǎn)半角
print(DBC2SBC(s))
# 半角轉(zhuǎn)全角
print(SBC2DBC(s))
s = ”'中文測試”'
# 全角轉(zhuǎn)半角
print(DBC2SBC(s))
# 半角轉(zhuǎn)全角
print(SBC2DBC(s))
四、總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
五、參考資料
http://thinkerou.com/2015-06/covert-dbc-sbc/
相關(guān)文章
Python由荷蘭數(shù)學(xué)和計(jì)算機(jī)科學(xué)研究學(xué)會(huì)的Guido van Rossum 于1990 年代初設(shè)計(jì),作為一門叫做ABC語言的替代品。 Python提供了高效的高級(jí)數(shù)據(jù)結(jié)構(gòu),還能簡單有效地面向?qū)ο缶幊?/div> 2021-10-10
python?pdfplumber庫批量提取pdf表格數(shù)據(jù)轉(zhuǎn)換為excel
這篇文章主要為大家介紹了python使用pdfplumber庫批量提取pdf表格數(shù)據(jù)轉(zhuǎn)換為excel格式的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
Python安裝使用命令行交互模塊pexpect的基礎(chǔ)教程
Pexpect是一個(gè)純Python模塊,可以用來和ssh、ftp、passwd、telnet等命令行命令進(jìn)行交互使用,在Linux系統(tǒng)下尤其好用,下面我們就來具體來看一下Python安裝使用命令行交互模塊pexpect的基礎(chǔ)教程:2016-05-05最新評(píng)論

