我正在尝试使用Python从字符串中删除特定字符。这就是我现在使用的代码。不幸的是,它似乎没有对字符串做任何事情。
for char in line:
if char in " ?.!/;:":
line.replace(char,'')我该如何正确地做到这一点?
发布于 2014-05-25 17:34:51
#!/usr/bin/python
import re
strs = "how^ much for{} the maple syrup? $20.99? That's[] ricidulous!!!"
print strs
nstr = re.sub(r'[?|$|.|!|a|b]',r' ',strs)#i have taken special character to remove but any #character can be added here
print nstr
nestr = re.sub(r'[^a-zA-Z0-9 ]',r'',nstr)#for removing special character
print nestrhttps://stackoverflow.com/questions/3939361
复制相似问题