Python3判断字符中英文数字符号

第一篇博客,写一下关于Python3中字符中英文数字符号的判断。

师姐给的一个任务,涉及到中英文混杂的文档集合,因而需要判断文本字符是中文还是英文。关于字符中文英的判断,网上一搜一大堆,但是拿过来一用就各种bug。主要原因是很多人给出的是Python2的代码,然后其中关于字符编码的问题,Pyhton2和Python3是不一样的。

判断字符中英文的方法一般是将字符放在Unicode编码中进行进行比较从而判断。

例如Unicode编码中中文的编码范围是’4e00’到’9fa5’,不过网上大多数的写法是:

1
2
3
4
5
def is_chinese(uchar):
if uchar >= u'u4e00' and uchar<=u'u9fa5':
return True
else:
return False

这里使用的u’u4e00’在Python3中行不通,正确的写法应该是:

1
2
3
4
5
def is_chinese(char):
if char >= '\u4e00' and char <= '\u9fa5':
return True
else:
return False

同样,判断字符是否为英文字母:

1
2
3
4
5
def is_alphabet(char):
if (char >= '\u0041' and char <= '\u005a') or (char >= '\u0061' and char <= '\u007a'):
return True
else:
return False

判断字符是否为数字:

1
2
3
4
5
def is_number(char):
if char >= '\u0030' and char <= '\u0039':
return True
else:
return False

判断字符是否非汉字,数字和英文字符:

1
2
3
4
5
def is_other(uchar):
if not (is_chinese(uchar) or is_number(uchar) or is_alphabet(uchar)):
return True
else:
return False

完整代码=>GitHub-AuxiliaryScripts-DistinguishChars.py.