简体   繁体   English

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

[英]python - How to remove punctuation in between words

I use the code to strip a line of text from punctuation: 我使用代码从标点符号中删除一行文本:

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

The problem is that words like doesn't turn to doesnt so now I want to remove the punctuation only between words but can't seem to figure out a way to do so. 问题是,像话doesn't转向doesnt所以现在我只想字之间去除标点符号,但似乎无法找出一种方法来做到这一点。 How should I go about this? 我该怎么办呢?

Edit: I thought about using the strip() function but that will only take effect on the right and left trailing of the whole sentence. 编辑:我想过使用strip()函数,但这只会影响整个句子的左右拖尾。

For example: 例如:

Isn't ., stackoverflow the - best ?

Should become: 应该成为:

Isn't stackoverflow the best

Instead of the current output: 而不是当前的输出:

Isnt stackoverflow the best

Assuming you consider words as groups of characters separated by spaces: 假设您将单词视为由空格分隔的字符组:

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

or 要么

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

这是你想要的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 python 删除标点符号和停用词 - how to remove punctuation and stop words using python 如何不计算单词间的标点符号 - How to not count punctuation between words 如何去除python中的标点符号? - How to remove punctuation in python? 计算Python标点符号之间的单词数 - Counting number of words between punctuation characters in Python 如何在列中的单词和标点符号之间添加空格? - How to add space between words and punctuation in a column? 如何在Python中删除中文标点符号 - How to remove Chinese punctuation in Python Python-如何通过空格将标点符号与单词分开,在标点符号和单词之间仅留一个空格? - Python - How do I separate punctuation from words by white space leaving only one space between the punctuation and the word? 如何在Python中将字符串拆分为单词(即使单词具有标点符号) - How to split string into words, even if words have punctuation, in Python Python:如何删除文本语料库中的标点符号,但不删除特殊词(例如c ++,c#、. net等) - Python: How remove punctuation in text corpus, but not remove it in special words (e.g. c++, c#, .net, etc) 如何使用停用词删除标点符号和不相关的词(文本挖掘) - How to remove punctuation and irrelevant words with stopwords (Text Mining)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM