【python脚本收集】全角半角转换
2016-01-25 21:36
706 查看
1.全角转半角
def strQ2B(ustring):
"""把字符串全角转半角"""
rstring = ""
for uchar in ustring:
inside_code=ord(uchar)
if inside_code==0x3000:
inside_code=0x0020
else:
inside_code-=0xfee0
if inside_code<0x0020 or inside_code>0x7e: #转完之后不是半角字符返回原来的字符
rstring += uchar
rstring += unichr(inside_code)
return rstring
2.半角转全角
def strB2Q(ustring):
"""把字符串半角转全角"""
rstring = ""
for uchar in ustring:
inside_code=ord(uchar)
if inside_code<0x0020 or inside_code>0x7e: #不是半角字符就返回原来的字符
rstring += uchar
if inside_code==0x0020: #除了空格其他的全角半角的公式为:半角=全角-0xfee0
inside_code=0x3000
else:
inside_code+=0xfee0
rstring += unichr(inside_code)
return rstring
def strQ2B(ustring):
"""把字符串全角转半角"""
rstring = ""
for uchar in ustring:
inside_code=ord(uchar)
if inside_code==0x3000:
inside_code=0x0020
else:
inside_code-=0xfee0
if inside_code<0x0020 or inside_code>0x7e: #转完之后不是半角字符返回原来的字符
rstring += uchar
rstring += unichr(inside_code)
return rstring
2.半角转全角
def strB2Q(ustring):
"""把字符串半角转全角"""
rstring = ""
for uchar in ustring:
inside_code=ord(uchar)
if inside_code<0x0020 or inside_code>0x7e: #不是半角字符就返回原来的字符
rstring += uchar
if inside_code==0x0020: #除了空格其他的全角半角的公式为:半角=全角-0xfee0
inside_code=0x3000
else:
inside_code+=0xfee0
rstring += unichr(inside_code)
return rstring
相关文章推荐
- python,numpy matplotlib WIN7 64位 安装教程
- Cisco Syslog 处理
- 【Python】抓取拉勾网全国Python的招聘信息
- python调试
- python 条件判断与循环
- python数字图像处理(15):霍夫线变换
- python 文件操作
- Python学习之--python概要
- python collections 系列
- Python、PIP环境变量的配置
- 让python在hadoop上跑起来
- python cmd命令调用
- Python Beautiful Soup模块的安装
- Python动态加载模块的3种方法
- 利用 Python 学习数据挖掘
- Python最佳入门教程列表(推荐)
- Python的各种小技巧
- Python爬虫
- python UI自动化 splinter
- 自学Python十二 战斗吧Scrapy!