2013-04-01 23 views
4

我用代碼從標點符號剝離一行文字:蟒蛇 - 如何刪除標點單詞之間

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

我認爲你需要更正式地解釋你的確切需求,用一些具體的「之前和之後」的例子。 – NPE

回答

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
line = line.translate(None, string.punctuation.replace('\'', '')) 

這是ü想什麼?