我想有效地从我的字符串中删除所有符号。x = hello!!r = dict.fromkeys(map(ord, '\n ' + string.punctuation))x.translate(r)我希望这会删除所有符号,而不仅仅是句号(。)
1 回答

智慧大石
TA贡献1946条经验 获得超3个赞
如何使用re.sub
删除所有string.punctuation
和' \n'
:
x = re.sub('|'.join(map(re.escape, string.punctuation + ' \n')), '', x)
如果您只想保留字母和数字字符,也可以使用以下正则表达式:
x = re.sub('[^a-zA-Z0-9]', '', x)
添加回答
举报
0/150
提交
取消