功能:

  • 判断unicode是否是汉字,数字,英文,或者是否是(汉字,数字和英文字符之外的)其他字符。
  • 全角、半角符号相互转换。

全角、半角?

全角--指一个字符占用两个标准字符位置。

汉字字符和规定了全角的英文字符及国标GB2312-80中的图形符号和特殊字符都是全角字符。一般的系统命令是不用全角字符的,只是在作文字处理时才会使用全角字符。

半角--指一字符占用一个标准的字符位置。

通常的英文字母、数字键、符号键都是半角的,半角的显示内码都是一个字节。在系统内部,以上三种字符是作为基本代码处理的,所以用户输入命令和参数时一般都使用半角。

例如:一个英文字符“ABC”如果以全角输入,会被当成汉字处理,如果以半角输入,会被当成普通英文字母处理。

# -*- coding: UTF-8 -*-

"""判断一个unicode是否是汉字"""
def is_chinese(uchar):
if uchar >= u'\u4e00' and uchar <= u'\u9fa5':
return True
else:
return False """判断一个unicode是否是数字"""
def is_number(uchar):
if uchar >= u'\u0030' and uchar <= u'\u0039':
return True
else:
return False """判断一个unicode是否是英文字母"""
def is_alphabet(uchar):
if (uchar >= u'\u0041' and uchar <= u'\u005a') or (uchar >= u'\u0061' and uchar <= u'\u007a'):
return True
else:
return False """判断是否是(汉字,数字和英文字符之外的)其他字符"""
def is_other(uchar):
if not (is_chinese(uchar) or is_number(uchar) or is_alphabet(uchar)):
return True
else:
return False """半角转全角"""
def B2Q(uchar):
inside_code = ord(uchar)
if inside_code < 0x0020 or inside_code > 0x7e: # 不是半角字符就返回原来的字符
return uchar
if inside_code == 0x0020: # 除了空格其他的全角半角的公式为:半角=全角-0xfee0
inside_code = 0x3000
else:
inside_code += 0xfee0
return unichr(inside_code) """全角转半角"""
def Q2B(uchar):
inside_code = ord(uchar)
if inside_code == 0x3000:
inside_code = 0x0020
else:
inside_code -= 0xfee0
if inside_code < 0x0020 or inside_code > 0x7e: # 转完之后不是半角字符返回原来的字符
return uchar
return unichr(inside_code) """把字符串全角转半角"""
def stringQ2B(ustring):
return "".join([Q2B(uchar) for uchar in ustring]) """将UTF-8编码转换为Unicode编码"""
def convert_toUnicode(string):
ustring = string
if not isinstance(string, unicode):
ustring = string.decode('UTF-8')
return ustring if __name__ == "__main__": ustring1 = u'收割季节 麦浪和月光 洗着快镰刀'
string1 = 'Sky0天地Earth1*' ustring1 = convert_toUnicode(ustring1)
string1 = convert_toUnicode(string1) for item in string1:
# print is_chinese(item)
# print is_number(item)
# print is_alphabet(item)
print is_other(item)

Python判断unicode是汉字,数字,英文,或者其他字符的更多相关文章

  1. python判断unicode是否是汉字,数字,英文,或者其他字符

    下面这个小工具包含了 判断unicode是否是汉字,数字,英文,或者其他字符. 全角符号转半角符号. unicode字符串归一化等工作. 还有一个能处理多音字的汉字转拼音的程序,还在整理中. #!/u ...

  2. php---------正则判断字符串中是否由汉字 数字 英文字母组成

    开发中常常用到正则表达式,分享两个常用的正则表达式,php检查字符串是否由汉字,数字,英文字母,下划线组成, 注意这里只是针对utf-8字符集的字符串检查. 数字 汉字 英文字母: if (!preg ...

  3. jquery判断字符长度 数字英文算1字符 汉字算2字符

    <input type="text" maxlength="25" oninput="textlength(this)"> &l ...

  4. Python 判断字符串是否为数字

    转载: http://www.runoob.com/python3/python3-check-is-number.html 以下实例通过创建自定义函数 is_number() 方法来判断字符串是否为 ...

  5. Python练习题 045:Project Euler 017:数字英文表达的字符数累加

    本题来自 Project Euler 第17题:https://projecteuler.net/problem=17 ''' Project Euler 17: Number letter coun ...

  6. python判断字符串是字母 数字 大小写

    字符串.isalnum() 所有字符都是数字或者字母,为真返回 Ture,否则返回 False. 字符串.isalpha() 所有字符都是字母,为真返回 Ture,否则返回 False. 字符串.is ...

  7. python 判断字符串是字母 数字 大小写还是空格

    str.isalnum()  所有字符都是数字或者字母,为真返回 Ture,否则返回 False. str.isalpha()   所有字符都是字母(当字符串为中文时, 也返回True),为真返回 T ...

  8. python 判断字符串是否以数字结尾

    import re def end_num(string): #以一个数字结尾字符串 text = re.compile(r".*[0-9]$") if text.match(st ...

  9. python判断字符串

    python判断字符串 s为字符串s.isalnum() 所有字符都是数字或者字母s.isalpha() 所有字符都是字母s.isdigit() 所有字符都是数字s.islower() 所有字符都是小 ...

随机推荐

  1. LeetCode:验证二叉搜索树【98】

    LeetCode:验证二叉搜索树[98] 题目描述 给定一个二叉树,判断其是否是一个有效的二叉搜索树. 假设一个二叉搜索树具有如下特征: 节点的左子树只包含小于当前节点的数. 节点的右子树只包含大于当 ...

  2. springmvc 需要用到的核心jar包

    aopbeanscontextcoreexpressionwebwebmvc

  3. iOS 自定义滑动切换TabbarItem 觉得设计丑也要做出来的UI效果。。。

    UI丑却要继续做的感言: 对UI不满意的时候,就会觉得丑爆了,时间长了,却丑习惯了. 论前一阵子Tabbar 多丑,丑得最后不要tabbar了...但是自定义tabbar 和遇到的问题解决的过程可以记 ...

  4. 开源流量分析系统 Apache Spot 概述(转)

    原文地址http://blog.nsfocus.net/apache-spot/ Apache Spot 是一个基于网络流量和数据包分析,通过独特的机器学习方法,发现潜在安全威胁和未知网络攻击能力的开 ...

  5. 什么是“欧几里德范数”(Euclidean norm)?

    x是n维向量(x1,x2,…,xn),||x||=根号(|x1|方+|x2|方+…+|xn|方) 补充:开平方,跟几何一样

  6. Effective C++ 条款01:视C++为一个语言联邦

    四个次语言 C Object-Oriented C++ Template C++ STL

  7. 记录使用Buildbot遇到的坑

    Buildbot Tips Buildbot也是个大坑..我并不熟悉python,偏偏文档又少.这几天使用buildbot出了不少坑.有的解决了,有的绕过去,这里都把它们一一记下来. Force Bu ...

  8. mysql CMD命令窗连接 - 转载

    cmd连接mysql的方法详解 首先需要进入mysql的安装文件夹bin目录下:cd + C:\Program Files\MySQL\MySQL Server 5.5\bin 连接:mysql -h ...

  9. SQL编码规范

    1        目的 为了保证所每个项目组编写出的程序都符合相同的规范,便于理解和维护,便于检查.减少出错概率,有助于成员间交流,保证一致性.统一性而建立的SQL程序编码规范. 2        范 ...

  10. java中获取服务器的IP和端口

    struts2 获取request HttpServletRequest requet=ServletActionContext.getRequest(); requet.getScheme()+&q ...