如何删除单词之间的标点符号

时间:2013-04-01 09:08:12

标签: python

我使用代码从标点符号中删除一行文本:

line = line.rstrip("\n")
line = line.translate(None, string.punctuation)

问题是像doesn't之类的字词会转到doesnt所以现在我只想删除单词之间的标点符号,但似乎无法找到方法。怎么样 我应该这样做吗?

编辑:我考虑过使用strip()函数,但这只会影响整个句子的左右尾部。

例如:

Isn't ., stackoverflow the - best ?

应该成为:

Isn't stackoverflow the best

而不是当前的输出:

Isnt stackoverflow the best

2 个答案:

答案 0 :(得分:11)

假设您将单词视为由空格分隔的字符组:

>>> from string import punctuation
>>> line = "Isn't ., stackoverflow the - best ?"
>>> ' '.join(word.strip(punctuation) for word in line.split() 
             if word.strip(punctuation))
"Isn't stackoverflow the best"

>>> line = "Isn't ., stackoverflow the - best ?"
>>> ' '.join(filter(None, (word.strip(punctuation) for word in line.split())))
"Isn't stackoverflow the best"

答案 1 :(得分:-1)

line = line.translate(None, string.punctuation.replace('\'', ''))

这是你想要的吗?