Python 判断字符串书否是纯ASCII (printable)

方法一: 正则

>>> import re

>>> # Printable
>>> print re.search(r‘[^\x20-\x7e]‘, ‘test‘)
None

>>> # Unprintable
>>> re.search(r‘[^\x20-\x7e]‘, ‘test\x00‘) != None
True

方法二:

import string
printset = set(string.printable)
isprintable = set(yourstring).issubset(printset)

原文详见:

Test if a python string is printable

相关推荐