判斷一個字符是否為漢字,英文字母,數字,空還是其他


# -*- 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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM