我正在尝试编写一个接受字符串(句子)然后清理它并返回所有字母、数字和连字符的函数。但是代码似乎出错了。请知道我在这里做错了什么。
示例:Blake D'souza is an !d!0t
应该返回:Blake D'souza is an d0t
Python:
def remove_unw2anted(str):
str = ''.join([c for c in str if c in 'ABCDEFGHIJKLNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890\''])
return str
def clean_sentence(s):
lst = [word for word in s.split()]
#print lst
for items in lst:
cleaned = remove_unw2anted(items)
return cleaned
s = 'Blake D\'souza is an !d!0t'
print clean_sentence(s)