在Python中检测非英文字符的字符串

TJ1*_*TJ1 27 python regex non-english

我有一些混合了英文和没有英文字母的字符串.例如:

w='_1991_??_??2'
Run Code Online (Sandbox Code Playgroud)

如何使用Regex或Python中的任何其他快速方法识别这些类型的字符串?

我不希望将字符串的字母逐个与字母列表进行比较,而是一次性快速地完成.

Sal*_*ali 56

您可以检查字符串是否只能用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&')
Run Code Online (Sandbox Code Playgroud)

  • 谢谢你的回答.在Python 3你所说的不正常工作,买我使用你建议的东西,用`s.encode('ascii')替换`s.decode('ascii')`,用`UnicodeEnecodeError`替换`UnicodeDecodeError`然后有效. (8认同)
  • 我编辑了这个答案,适用于python 2和3. (3认同)
  • @ TJ1,你的方法对于Python3是正确的,你只是有一个错字 - 它是```UnicodeEncodeError``` (2认同)

Tor*_*llo 22

恕我直言,这是最简单的解决方案:

def isEnglish(s):
  return s.isascii()

print(isEnglish("Test"))
print(isEnglish("_1991_??_??2"))

Output:
True
False
Run Code Online (Sandbox Code Playgroud)

  • `isacii` 是在 python 3.7 中引入的。所以使用这个函数的最低要求你必须> = python 3.7 (3认同)

Kat*_*ova 11

如果您使用字符串(而不是unicode对象),您可以使用转换和检查来清除它isalnum(),这比抛出异常更好:

import string

def isEnglish(s):
    return s.translate(None, string.punctuation).isalnum()


print isEnglish('slabiky, ale liší se podle významu')
print isEnglish('English')
print isEnglish('?? ???????? ?? ?????? ??')
print isEnglish('how about this one : ? asf?')
print isEnglish('?fd4))45s&')
print isEnglish('????? ?? ???????')

> False
> True
> False
> False
> True
> False
Run Code Online (Sandbox Code Playgroud)

您还可以使用此函数从字符串中过滤非ascii字符:

ascii = set(string.printable)   

def remove_non_ascii(s):
    return filter(lambda x: x in ascii, s)


remove_non_ascii('slabiky, ale liší se podle významu')
> slabiky, ale li se podle vznamu
Run Code Online (Sandbox Code Playgroud)

  • 您好,虽然这个解决方案看起来不错(我希望尽可能避免异常),但它不能识别所有英文字符。甚至“Space”也无法识别。 (2认同)

roi*_*363 8

我相信这个运行时间会很短,因为一旦它发现一个不是拉丁字母的字符就会停止。它还使用生成器来更好地使用内存。

\n\n
import string\n\ndef has_only_latin_letters(name):\n    char_set = string.ascii_letters\n    return all((True if x in char_set else False for x in name))\n\n>>> has_only_latin_letters(\'_1991_\xd8\xa7\xd9\x81_\xd8\xac\xd9\x8a2\')\nFalse\n>>> has_only_latin_letters(\'bla bla\')\nTrue\n>>> has_only_latin_letters(\'bl\xc3\xa4 bl\xc3\xa4\')\nFalse\n>>> has_only_latin_letters(\'\xec\xa0\x80\xec\xa3\xbc\xec\xa4\x91\xec\x95\x99\xec\xb4\x88\xeb\x93\xb1\xed\x95\x99\xea\xb5\x90\')\nFalse\n>>> has_only_latin_letters(\'also a string with numbers and punctuation 1, 2, 4\')\nTrue\n
Run Code Online (Sandbox Code Playgroud)\n\n

您还可以使用不同的字符集:

\n\n
>>> string.ascii_letters\n\'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\'\n\n>>> string.ascii_lowercase\n\'abcdefghijklmnopqrstuvwxyz\'\n\n>>> string.ascii_uppercase\n\'ABCDEFGHIJKLMNOPQRSTUVWXYZ\'\n\n>>> string.punctuation\n\'!"#$%&\\\'()*+,-./:;<=>?@[\\\\]^_`{|}~\'\n\n>>> string.digits\n\'0123456789\'\n\n>>> string.digits + string.lowercase\n\'0123456789abcdefghijklmnopqrstuvwxyz\'    \n\n>>> string.printable\n\'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%& \n\\\'()*+,-./:;<=>?@[\\\\]^_`{|}~ \\t\\n\\r\\x0b\\x0c\'\n
Run Code Online (Sandbox Code Playgroud)\n\n

要添加拉丁重音字母,可以参考这篇文章

\n


小智 6

import re

english_check = re.compile(r'[a-z]')

if english_check.match(w):
    print "english",w
else:
    print "other:",w
Run Code Online (Sandbox Code Playgroud)

  • 诸如“天真”或“陈词滥调”之类的词又如何呢? (6认同)