前言
本文主要给大家介绍了关于python3中全角和半角字符转换的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。
一、背景介绍
解决什么问题:快速方便的对文本进行全角半角自动转换
适用什么场景:学生答题数据中全角字符替换为半角字符
二、全角半角原理
全角即:Double Byte Character,简称DBC
半角即:Single Byte Character,简称SBC
在 windows 中,中文和全角字符都占两个字节,并且使用了 ascii chart 2 (codes 128–255);
全角字符的第一个字节总是被置为 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 -*- # i@mail.chenpeng.info ”' 全角即: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', ‘(' => ‘(‘, ‘)' => ‘)', ‘〔' => ‘[‘, ‘〕' => ‘]', ‘【' => ‘[‘, ‘】' => ‘]', ‘〖' => ‘[‘, ‘〗' => ‘]', ‘”‘ => ‘[‘, ‘”‘ => ‘]', ‘\” => ‘[‘, ‘\” => ‘]', ‘{' => ‘{‘, ‘}' => ‘}', ‘《' => ‘<‘, ‘》' => ‘>', ‘%' => ‘%', ‘+' => ‘+', ‘—' => ‘-‘, ‘-' => ‘-‘, ‘~' => ‘-‘, ‘:' => ‘:', ‘。' => ‘.', ‘、' => ‘,', ‘,' => ‘.', ‘、' => ‘.', ‘;' => ‘,', ‘?' => ‘"color: #ff0000">四、总结以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。
五、参考资料
http://thinkerou.com/2015-06/covert-dbc-sbc/
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
暂无评论...
更新日志
2024年11月25日
2024年11月25日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]