如何用Python删除字符串中的符号?

时间:2009-05-18 01:55:38

标签: python regex string

我是Python和RegEx的初学者,我想知道如何创建一个带符号的字符串并用空格替换它们。任何帮助都很棒。

例如:

how much for the maple syrup? $20.99? That's ricidulous!!!

成:

how much for the maple syrup 20 99 That s ridiculous

3 个答案:

答案 0 :(得分:115)

单向,使用regular expressions

>>> s = "how much for the maple syrup? $20.99? That's ridiculous!!!"
>>> re.sub(r'[^\w]', ' ', s)
'how much for the maple syrup   20 99  That s ridiculous   '
  • \w将匹配字母数字字符和下划线

  • [^\w]将匹配字母数字或下划线的任何内容

答案 1 :(得分:24)

有时需要更长的时间来弄清楚正则表达式而不是在python中写出来:

import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
    s = s.replace(char, ' ')

如果您需要其他字符,可以将其更改为使用白名单或扩展黑名单。

示例白名单:

whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
    if char in whitelist:
        new_s += char
    else:
        new_s += ' '

使用generator-expression示例白名单:

whitelist = string.letters + string.digits + ' '
new_s = ''.join(c for c in s if c in whitelist)

答案 2 :(得分:7)

我经常打开控制台并在对象方法中寻找解决方案。它经常出现在那里:

>>> a = "hello ' s"
>>> dir(a)
[ (....) 'partition', 'replace' (....)]
>>> a.replace("'", " ")
'hello   s'

简答:使用string.replace()