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

标签 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

最佳答案

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

>>> 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/

相关文章:

python - Apache 运行静态内容和 Flask 应用程序

python - 具有继承的包中的循环导入依赖项

python - 如何解决在本地运行我的 python 代码的问题?

python - 如何获取 Bokeh 小部件事件和属性的列表(可用于触发 Python 回调)

python - 通过 sklearn.metrics.make_scorer 将估算器传递给自定义评分函数

python - 从工作目录中的文件夹导入另一个 py 文件

python - gspread 更新不在范围内的多个单元格

python - 比较和更新 pandas dataframe 列列表与来自另一列的字符串

python - Python中的 "2*2"和 "2**2"有什么区别?

Python:合并两个任意数据结构