您现在的位置是:首页 > 电脑 > 

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

2025-07-28 10:05:45
如何删除单词之间的标点符号(python - How to remove punctuation in between words) 我使用代码从标点符号中删除一行文本: line = line.rstrip("\n") line = line.translate(one, string.punctuation) 问题是,像现在这样的话doesn't
如何删除单词之间的标点符号(python - How to remove punctuation in between words)

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

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

问题是,像现在这样的话doesn't转向doesnt现在,我现在想删除单词之间的标点符号,但似乎无法到一种方法来做到这一点。 我应该怎么做呢?

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

例如:

Isn't ., stackoverflow the - best ?

应该成为:

Isn't stackoverflow the best

而不是当前的输出:

Isnt stackoverflow the best

I use the code to strip a line of text from punctuation:

line = line.rstrip("\n") line = (one, 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. 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.

For example:

Isn't ., stackoverflow the - best ?

Should become:

Isn't stackoverflow the best

Instead of the current output:

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(one, (word.strip(punctuation) for word in line.split()))) "Isn't stackoverflow the best"

Assuming you cider 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(one, (word.strip(punctuation) for word in line.split()))) "Isn't stackoverflow the best"

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/diannao/654002.html

相关标签:无
上传时间: 2023-07-28 12:46:00
留言与评论(共有 19 条评论)
本站网友 公分厘米
19分钟前 发表
Isnt stackoverflow the best 最满意答案 假设您将单词视为由空格分隔的字符组: >>> from string import punctuation >>> line = "Isn't .
本站网友 怎样去除额头皱纹
21分钟前 发表
(word.strip(punctuation) for word in line.split()))) "Isn't stackoverflow the best"
本站网友 回马枪是什么意思
13分钟前 发表
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 .
本站网友 你被猴打过
11分钟前 发表
stackoverflow the - best ?" >>> ' '.join(filter(one
本站网友 北京万科金域缇香
30分钟前 发表
例如: Isn't .
本站网友 天祥菩提
1分钟前 发表
Isnt stackoverflow the best 最满意答案 假设您将单词视为由空格分隔的字符组: >>> from string import punctuation >>> line = "Isn't .
本站网友 网上外卖
3分钟前 发表
我应该怎么做呢? 编辑:我想过使用strip()函数
本站网友 双眼皮贴
9分钟前 发表
I thought about using the strip() function but that will only take effect on the right and left trailing of the whole sentence. For example
本站网友 地铁线
18分钟前 发表
stackoverflow the - best ?" >>> ' '.join(word.strip(punctuation) for word in line.split() if word.strip(punctuation)) "Isn't stackoverflow the best" 要么 >>> line = "Isn't .
本站网友 林钢
17分钟前 发表
stackoverflow the - best ?" >>> ' '.join(word.strip(punctuation) for word in line.split() if word.strip(punctuation)) "Isn't stackoverflow the best" 要么 >>> line = "Isn't .
本站网友 云知识
9分钟前 发表
(word.strip(punctuation) for word in line.split()))) "Isn't stackoverflow the best"
本站网友 驼鸟政策
1分钟前 发表
>>> from string import punctuation >>> line = "Isn't .
本站网友 lingo下载
20分钟前 发表
stackoverflow the - best ?" >>> ' '.join(filter(one
本站网友 七日瘦身汤
29分钟前 发表
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 .
本站网友 深圳油价
29分钟前 发表
stackoverflow the - best ? Should become
本站网友 无线充电器
0秒前 发表
例如: Isn't .
本站网友 4444hhh
16分钟前 发表
(word.strip(punctuation) for word in line.split()))) "Isn't stackoverflow the best"
本站网友 陈环保
28分钟前 发表
像现在这样的话doesn't转向doesnt现在