我想检查一个字符串是否是ASCII格式的。
我知道ord(),但是当我尝试ord('é')时,我有TypeError: ord()期望一个字符,但发现长度为2的字符串。我知道这是由我构建Python的方式引起的(如ord()的文档所解释的那样)。
还有别的办法吗?
我想检查一个字符串是否是ASCII格式的。
我知道ord(),但是当我尝试ord('é')时,我有TypeError: ord()期望一个字符,但发现长度为2的字符串。我知道这是由我构建Python的方式引起的(如ord()的文档所解释的那样)。
还有别的办法吗?
当前回答
我觉得你问的问题不对
python中的字符串没有对应于'ascii'、utf-8或任何其他编码的属性。字符串的来源(无论是从文件读取,还是从键盘输入,等等)可能已经用ascii编码了一个unicode字符串来生成字符串,但这是您需要去寻找答案的地方。
也许你会问:“这个字符串是用ascii编码unicode字符串的结果吗?”——这个你可以回答 通过:
try:
mystring.decode('ascii')
except UnicodeDecodeError:
print "it was not a ascii-encoded unicode string"
else:
print "It may have been an ascii-encoded unicode string"
其他回答
Python 3.7新增功能(bpo32677)
不再对字符串进行烦人/低效的ascii检查,新的内置str/bytes/bytearray方法- .isascii()将检查字符串是否为ascii。
print("is this ascii?".isascii())
# True
您可以使用正则表达式库,它接受Posix标准[[:ASCII:]]定义。
我使用以下方法来确定字符串是ascii还是unicode:
>> print 'test string'.__class__.__name__
str
>>> print u'test string'.__class__.__name__
unicode
>>>
然后使用一个条件块来定义函数:
def is_ascii(input):
if input.__class__.__name__ == "str":
return True
return False
这样做怎么样?
import string
def isAscii(s):
for c in s:
if c not in string.ascii_letters:
return False
return True
就像@RogerDahl的回答一样,但是通过否定字符类和使用搜索而不是find_all或match来短路更有效。
>>> import re
>>> re.search('[^\x00-\x7F]', 'Did you catch that \x00?') is not None
False
>>> re.search('[^\x00-\x7F]', 'Did you catch that \xFF?') is not None
True
我想正则表达式对此进行了很好的优化。