python:清理一个字符串

l--*_*''' 2 python string

我有一个像这样的字符串

somestring='in this/ string / i have many. interesting.occurrences of {different chars} that need     to .be removed  '
Run Code Online (Sandbox Code Playgroud)

这是我想要的结果:

somestring='in this string i have many interesting occurrences of different chars that need to be removed'
Run Code Online (Sandbox Code Playgroud)

我开始手动做各种各样的.replace,但有很多不同的组合,我认为必须有一个更简单的方法.也许有一个图书馆已经这样做了?

有谁知道我怎么可以清理这个字符串>?

ken*_*ytm 15

我会使用正则表达式将所有非字母数字替换为空格:

>>> import re
>>> somestring='in this/ string / i have many. interesting.occurrences of {different chars} that need     to .be removed  '
>>> rx = re.compile('\W+')
>>> res = rx.sub(' ', somestring).strip()
>>> res
'in this string i have many interesting occurrences of different chars that need to be removed'
Run Code Online (Sandbox Code Playgroud)

  • @user:这只是一个简单的正则表达式.该库位于http://docs.python.org/library/re.html.有关正则表达式的更多信息,请参见http://www.regular-expressions.info/. (3认同)