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

时间:2019-02-21 10:01:34

标签: python

我有一个如下列表:

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']]    

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分:0)

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