***如何判断一个字符是否为汉字,英文字母,数字,空还是其他

# -*- coding: utf-8 -*-
# @Time    : 18-9-13 下午12:16
# @Author  : Felix Wang

# 判断一个字符是否为汉字,英文字母,数字,空还是其他
# 使用Unicode编码来判断
def is_chinese(uchar):
    """判断一个unicode是否是汉字"""
    if  u'\u4e00' <= uchar <= u'\u9fa5':
        return True
    else:
        return False


def is_number(uchar):
    """判断一个unicode是否是数字"""
    if u'\u0030' <= uchar <= u'\u0039':
        return True
    else:
        return False


def is_alphabet(uchar):
    """判断一个unicode是否是英文字母"""
    if (u'\u0041' <= uchar <= u'\u005a') or (u'\u0061' <= uchar <= u'\u007a'):
        return True
    else:
        return False


def is_space(uchar):
    """判断一个unicode是否是空字符串(包括空格,回车,tab)"""
    space = [u'\u0020', u'\u000A', u'\u000D', u'\u0009']
    if uchar in space:
        return True
    else:
        return False


def is_other(uchar):
    """判断是否非汉字,数字,空字符和英文字符"""
    if not (is_space(uchar) or is_chinese(uchar) or is_number(uchar) or is_alphabet(uchar)):
        return True
    else:
        return False
  

猜你喜欢

转载自www.cnblogs.com/zpf666/p/9665030.html