2019独角兽企业重金招聘Python工程师标准>>>

def is_chinese(uchar):"""判断一个unicode是否是汉字"""if uchar >= u'\u4e00' and uchar<=u'\u9fa5':return Trueelse:return Falsedef is_number(uchar):"""判断一个unicode是否是数字"""if uchar >= u'\u0030' and uchar<=u'\u0039':return Trueelse:return Falsedef is_alphabet(uchar):"""判断一个unicode是否是英文字母"""if (uchar >= u'\u0041' and uchar<=u'\u005a') or (uchar >= u'\u0061' and uchar<=u'\u007a'):return Trueelse:return Falsedef is_other(uchar):"""判断是否非汉字,数字和英文字符"""if not (is_chinese(uchar) or is_number(uchar) or is_alphabet(uchar)):return Trueelse:return Falsedef B2Q(uchar):"""半角转全角"""inside_code=ord(uchar)if inside_code<0x0020 or inside_code>0x7e:      #不是半角字符就返回原来的字符return ucharif inside_code==0x0020: #除了空格其他的全角半角的公式为:半角=全角-0xfee0inside_code=0x3000else:inside_code+=0xfee0return unichr(inside_code)def Q2B(uchar):"""全角转半角"""inside_code=ord(uchar)if inside_code==0x3000:inside_code=0x0020else:inside_code-=0xfee0if inside_code<0x0020 or inside_code>0x7e:      #转完之后不是半角字符返回原来的字符return ucharreturn unichr(inside_code)def stringQ2B(ustring):"""把字符串全角转半角"""return "".join([Q2B(uchar) for uchar in ustring])def uniform(ustring):"""格式化字符串,完成全角转半角,大写转小写的工作"""return stringQ2B(ustring).lower()def string2List(ustring):"""将ustring按照中文,字母,数字分开"""retList=[]utmp=[]for uchar in ustring:if is_other(uchar):if len(utmp)==0:continueelse:retList.append("".join(utmp))utmp=[]else:utmp.append(uchar)if len(utmp)!=0:retList.append("".join(utmp))return retListif __name__=="__main__":#test Q2B and B2Qfor i in range(0x0020,0x007F):print Q2B(B2Q(unichr(i))),B2Q(unichr(i))#test uniformustring=u'中国 人名a高频A'ustring=uniform(ustring)ret=string2List(ustring)print ret

转载于:https://my.oschina.net/eonezhang/blog/125440

python字符串编码判断相关推荐

  1. python字符串类型判断(python中datatype()测试字符串类型)

    python如何判断变量是否是字符串 Python中的数据类型有数字.字符串,列表.元组.字典.集合等. 相关推荐:<Python教程> python中,判断某变量的数据类型是否为字符串, ...

  2. Python字符串编码坑彻底详细解决 何梁

    所以不同编码的str对象可以先解码(decode)成unicode 再编码(encode)成其他编码的str对象. 真正完全搞清楚Python的编码问题 我想大家经常被Python的编码问题搞的晕头转 ...

  3. python 字符串 编码 解码_Python 字符串编解码研究

    Python 2.X 在输入汉字和特殊字符的时候,经常遇到编码解码的问题,究其原因,编译器默认将文件当做ascii编码,因此要正确的实现编解码的转换,需要进行一些设置. 首先让我们来了解几个概念. 文 ...

  4. python 字符串编码

    通过字符串的decode和encode方法 1 encode([encoding,[errors]]) #其中encoding可以有多种值,比如gb2312 gbk gb18030 bz2 zlib ...

  5. [转载] python字符串_一文详解Python字符串条件判断方法

    参考链接: Python字符串| isdecimal 作者 | 张小吉 来源 | 鸡仔说(ID:jizaishuo) 前言 人喜欢为自己的错误,找各种借口开脱.本周做算法题leetcode<39 ...

  6. python字符串编码及乱码解决方案

    http://blog.csdn.net/pipisorry/article/details/44136297 字符编码详解 [字符编码ASCII,Unicode和UTF-8] 主要非英文字符集的编码 ...

  7. Python字符串编码检测

    字符串编码产生的乱码着实让人头疼-- 关于字符串和编码的知识,可以参考廖雪峰大大的博客字符串和编码 那么有没有办法完全正确的检测字符集编码呢?答案是:没有!参考how-to-determine-the ...

  8. python字符串编码_Python字符串编码答疑

    Python 2中的字符串分类 在Python 2中字符串,有两个类型,一个是str,一个是unicode.str可以理解为ASCII的字符列表,说白了,只能存储ASCII字符,如果赋个中文值,会报错 ...

  9. python 字符串编码与解码

    1. 字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码,即先将其他编码的字符串解码(decode)成unicode,再从unicode编码 ...

最新文章

  1. Exchange动态同步中的INTERNET_29错误代码
  2. 首个ML的生物医药大型数据集,3行代码就能运行丨哈佛MIT斯坦福等出品
  3. Python二十个小技巧
  4. Linux 中yum的配置
  5. sql交叉报表实例(转)
  6. fetch first mysql_MySQL多版本并发控制机制(MVCC)源码浅析
  7. Pytorch DataLoader一次性封装多种数据集(pytorch Data学习六)
  8. (转)淘淘商城系列——搜索系统搭建
  9. 台式计算机关机后自行重启,台式机电脑总是自动关机重启该怎么解决?
  10. neo4j 学习-2
  11. php mysql时间倒序,php mysql时间已过去计算
  12. 《手机测试Robotium实战教程》——第2章,第2.2节Eclipse的安装
  13. 李宏毅机器学习——无监督学习(一)
  14. GitHub 打钱了,10 万美元!
  15. ldd usr bin mysql_ldd与otool
  16. QQ IP 地址查询相关
  17. 论文文献综述的题目是什么样的,应该怎么写?
  18. 悲伤是一种毒,会上瘾
  19. OpenCASCADE 读写STEP文件
  20. unity新动画系统之动画层和动画遮罩

热门文章

  1. 北京小哥在日本召唤出机器飞龙,拿下ICRA 2018最佳无人机论文
  2. 我对软件发展的思考,一个不变却一直在变的话题
  3. java中的继承(一)
  4. 错误:找不到或无法加载主类
  5. 数据分析融入至BI工具的新思路
  6. 第3章:Hadoop分布式文件系统(1)
  7. Inconsistency detected Invalid view holder adapter position
  8. 贵安新区构建大数据+物联网+智能制造产业链
  9. VMware Linux VDI 安装步骤
  10. openstack版本历史