我使用代码从标点符号中删除一行文本:
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
最佳答案
假设您将单词视为由空格分隔的字符组:
>>> 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"
关于python - 如何删除单词之间的标点符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15740579/