【发布时间】:2020-10-03 19:14:27
【问题描述】:
我有一个单词列表,我想删除所有特殊字符和数字,这是我想出来的:
输入: #将所有单词转换为小写
words = [word.lower() for word in words]
print(words[:100])
输出:
['rt', '@', 'dark', 'money', 'has', 'played', 'a', 'significant', 'role', 'in', 'the', 'overall', 'increase', 'of', 'election', 'spending', 'in', 'state', 'judicial', 'elections.', 'https://e85zq', 'rt', '@', 'notice,', 'women,', 'how', 'you', 'are', 'always', 'the', 'target', 'of', 'democrats’', 'fear', 'mongering', 'in', 'an', 'election', 'year', 'or', 'scotus', 'confirmation.', 'it', 'is', 'not', 'because', 'our', 'rights', 'are', 'actually', 'at', 'risk.', 'it', 'is', 'because', 'we', 'are', 'easily', 'manipulated.', 'goes', 'allll', 'the', 'way', 'back', 'to', 'eve.', 'resist', 'hysteria', '&', 'think.', 'rt', '@', 'oct', '5:', 'last', 'day', 'to', 'register', 'to', 'vote.', 'oct', '13:', 'early', 'voting', 'starts.', 'oct', '23:', 'last', 'day', 'to', 'request', 'a', 'mail-in', 'ballot.', 'nov', '3:', 'election', 'day', 'rt', '@']
输入
words_cleaned = [re.sub(r"[-()\"#/@;:<>{}`+=~|.!?,]", "", i) for i in words]
print(words_cleaned[:100])
输出
我最终得到一个空字符串 []
我需要删除'@'之类的字符,并将'@test'之类的字符转换为'test'。有什么想法吗?
【问题讨论】:
-
@PranavHosangadi:我可以建议你阅读正则表达式吗?
.在字符类[ ... ]中时不表示任何字符。 -
你好@andrew-seaman,你的代码在我的笔记本上完美运行。您可以再次尝试您的代码或在此处上传您的整个代码吗?
-
@LukeWoodward 那不是我脸上的鸡蛋! OP,您的代码工作正常。请附上完整的minimal reproducible example 以重现您的问题
-
''.join(e for e in string if e.isalpha())
标签: python list special-characters