您只需检查字符串是否只能使用ASCII字符(拉丁字母+其他字符)进行编码。如果它不能被编码,则它具有来自其他字母的字符。
注意评论# -*- coding: ....
。它应该在python文件的顶部(否则您会收到一些关于编码的错误)
# -*- coding: utf-8 -*-
def isEnglish(s):
try:
s.encode(encoding='utf-8').decode('ascii')
except UnicodeDecodeError:
return False
else:
return True
assert not isEnglish('slabiky, ale li?í se podle významu')
assert isEnglish('English')
assert not isEnglish('?? ???????? ?? ?????? ??')
assert not isEnglish('how about this one : 通 asf?')
assert isEnglish('?fd4))45s&')