您可以使用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'