使用python删除所有特殊字符和数字

问题描述 投票:-3回答:2

我有一个如下列表:

issue=[[hi iam !@going $%^ to uk&*(us \\r\\ntomorrow {morning} by the_way 
        two-three!~`` [problems]:are there;]
      [happy"journey" and \\r\\n\\rbring 576 chachos?><,.|\/)]]

我尝试了下面的代码,但我没有获得所需的输出:

import re
ab=re.sub('[^A-Za-z0-9]+', '', issue)
bc=re.split(r's, ab)

我想看看如下输出:

issue_output=[['hi' 'iam' 'going' 'to' 'uk' 'us' 'tomorrow' 'morning' 'by' 
                'the' 'way' 'two' 'three' 'problems' 'are' 'there']
              [ 'happy' 'journey' 'and' 'bring' 'chachos']]    
python
2个回答
0
投票

用空格替换所有不需要的字符。然后通过单个空格去掉多个空格。

issue = '[[hi iam !@going $%^ to uk&*(us tomorrow {morning} by the_way two-three!~`` problems:are there;],[happy"journey" and bring 576chachos?><,.|\/)]]'
tmp = "".join(x if x.isalpha() or x.isspace() else " " for x in issue)
result = " ".join(tmp.split())
print(result)

如果你想要方括号:

tmp = "".join(x if x.isalpha() or x.isspace() or x in ["[", "]"] else " " for x in issue)

0
投票

使用re.sub('[^ A-Za-z] +','',问题)

© www.soinside.com 2019 - 2024. All rights reserved.