这是我的字符串:蟒蛇删除怪异撇号和其他怪异字符无法在string.punctuation
mystring = "How’s it going?"
这是我做过什么:
import string
exclude = set(string.punctuation)
def strip_punctuations(mystring):
for c in string.punctuation:
new_string=''.join(ch for ch in mystring if ch not in exclude)
new_string = chat_string.replace("\xe2\x80\x99","")
new_string = chat_string.replace("\xc2\xa0\xc2\xa0","")
return chat_string
OUTPUT:
如果我没有包括这一行new_string = chat_string.replace("\xe2\x80\x99","")
这将是输出:
'How\xe2\x80\x99s it going'
我意识到 排除没有在列表中怪异的撇号:
print set(exclude)
set(['!', '#', '"', '%', '$', "'", '&', ')', '(', '+', '*', '-', ',', '/', '.', ';', ':', '=', '<', '?', '>', '@', '[', ']', '\\', '_', '^', '`', '{', '}', '|', '~'])
如何确保所有这些字符都取出来,而不是手动在未来替代它们?
Python 2,我假设? –
yep python 2.7。 – jxn
您不应该将字符串作为utf8字符串。先解码它们。 – Daniel