删除非字母字符python_Python:尝试降低字符串并删除空格以外的非字母数字字符...

您可以使用str.translate删除标点符号:

s = 'Hi, you!'

from string import punctuation

print(s.translate(None,punctuation).lower())

hi you

对于python3:

s = 'Hi, you!'

from string import punctuation

print(s.translate({ord(k):"" for k in punctuation}).lower())

hi you

在一个功能:

from string import punctuation

def remove_punctuation(s):

return s.translate(None,punctuation).lower()

def remove_punctuation(s):

return s.translate({ord(k): "" for k in punctuation}).lower()

输出:

In [3]: remove_punctuation(' No under_score!')

Out[3]: ' no underscore'

In [4]: remove_punctuation('Hi, you!')

Out[4]: 'hi you'

如果要删除前导空格,请添加条带 .

from string import punctuation

def remove_punctuation(s):

return s.translate(None,punctuation).lower().strip()

输出:

In [6]: remove_punctuation(' No under_score!')

Out[6]: 'no underscore'

In [7]: remove_punctuation('Hi, you!')

Out[7]: 'hi you'

你可能感兴趣的:(删除非字母字符python)