繁体   English   中英

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

[英]remove all special characters and numbers using 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']]    

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

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)

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM