有没有办法检查所有特殊字符?

时间:2015-10-29 00:05:13

标签: python methods

有没有办法测试python中的所有特殊字符而不是手动输入它们,可能类似于.isalnum或.isalpha函数?我对编码比较陌生,所以我不知道。

2 个答案:

答案 0 :(得分:1)

假设任何非字母数字字符都是特殊字符,您可以在前面<a href="#" id="hamburger-icon" title="Menu"> <span class="line line-1"></span> <span class="line line-2"></span> <span class="line line-3"></span> 添加not,并在有任何特殊字符时返回true:

isalnum

答案 1 :(得分:0)

您可以定义自己的is_alphanumeric功能:

from string import digits, letters

def is_alphanumeric(mystring):
    """ Returns true if all characters of `mystring` are either letters or digits:
    >>> is_alphanumeric('hello wörld')
    True
    >>> is alphanumeric('Hello World!')
    False
    """
    return all(character in chain(digits, letters) for character in mystring)

如果要将其限制为ascii:

from string import digits, letters_ascii

def is_alphanumeric_ascii(mystring):
    """ Returns true if all characters of `mystring` are either ascii    letters or digits: 
    >>> is_alphanumeric('hello wörld')
    False
    >>> is alphanumeric('Hello World')
    True
    """
    return all(character in chain(digits, letters_ascii) for character in mystring)