利用字符串的decode方法,如果不是对应字符编码,报异常,然后返回False
- 是否为ascii字符:
def is_ascii(s): try: if s.decode('ascii'): return True except Exception as e: return False
- 是否为ascii字符中字母数字:
def is_alnum(s):
try:
if s.decode('ascii').isalnum():
return True
except Exception as e:
return False
return False