未加星标

python3实现全角和半角字符转换的方法示例

字体大小 | |
[开发(python) 所属分类 开发(python) | 发布者 店小二05 | 时间 | 作者 红领巾 ] 0人收藏点击收藏

前言

本文主要给大家介绍了关于python3中全角和半角字符转换的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。

一、背景介绍
解决什么问题:快速方便的对文本进行全角半角自动转换
适用什么场景:学生答题数据中全角字符替换为半角字符
二、全角半角原理
全角即:Double Byte Character,简称DBC
半角即:Single Byte Character,简称SBC
windows 中,中文和全角字符都占两个字节,并且使用了 ascii chart 2 (codes 128C255);
全角字符的第一个字节总是被置为 163,而第二个字节则是相同半角字符码加上128(不包括空格,全角空格和半角空格也要考虑进去);
对于中文来说,它的第一个字节被置为大于163,如'阿'为:176 162,检测到中文时不进行转换。
例如:半角 a 为 65,则全角 a 是 163(第一个字节)、193(第二个字节,128+65)。

全角半角示例:(文本 test.txt 包含全角和半角字符)

F:\test>type test.txt
123456
123456
abcdefg
abcdefg
中国你好

三、使用 Python3 实现全角半角转换

# -*- coding:utf-8 -*-
# [email protected]o
”'
全角即:Double Byte Character,简称:DBC
半角即:Single Byte Character,简称:SBC
”'
def DBC2SBC(ustring):
”' 全角转半角 ”'
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):
”' 半角转全角 ”'
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', ‘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',
‘(' => ‘(‘, ‘)' => ‘)', ‘〔' => ‘[‘, ‘〕' => ‘]', ‘【' => ‘[‘,
‘】' => ‘]', ‘〖' => ‘[‘, ‘〗' => ‘]', ‘”‘ => ‘[‘, ‘”‘ => ‘]',
‘\” => ‘[‘, ‘\” => ‘]', ‘{' => ‘{‘, ‘}' => ‘}', ‘《' => ‘<‘,
‘》' => ‘>',
‘%' => ‘%', ‘+' => ‘+', ‘―' => ‘-‘, ‘-' => ‘-‘, ‘~' => ‘-‘,
‘:' => ‘:', ‘。' => ‘.', ‘、' => ‘,', ‘,' => ‘.', ‘、' => ‘.',
‘;' => ‘,', ‘?' => ‘?', ‘!' => ‘!', ‘…' => ‘-‘, ‘‖' => ‘|',
‘”‘ => ‘”‘, ‘\” => ‘`', ‘\” => ‘`', ‘|' => ‘|', ‘〃' => ‘”‘,
‘ ' => ‘ ‘);
”'
# 全角转半角
print(DBC2SBC(s))
# 半角转全角
print(SBC2DBC(s))
s = ”'中文测试”'
# 全角转半角
print(DBC2SBC(s))
# 半角转全角
print(SBC2DBC(s))

四、总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。

五、参考资料

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

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

主题: 谢大数据Python
分页:12
转载请注明
本文标题:python3实现全角和半角字符转换的方法示例
本站链接:http://www.codesec.net/view/565023.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(27)