我有一个像这样的字符串
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)
| 归档时间: |
|
| 查看次数: |
14468 次 |
| 最近记录: |