[英]Python - How do I separate punctuation from words by white space leaving only one space between the punctuation and the word?
我有以下字符串:
input = "I love programming with Python-3.3! Do you? It's great... I give it a 10/10. It's free-to-use, no $$$ involved!"
所有标点符号都应与单词“ /”,“'',“-”,“ +”和“ $”除外。
因此输出应为:
"I love programming with Python-3 . 3 ! Do you ? It's great . . . I give it a 10/10. It's free-to-use , no $$$ involved !"
我使用以下代码:
for x in string.punctuation:
if x == "/":
continue
if x == "'":
continue
if x == "-":
continue
if x == "+":
continue
if x == "$":
continue
input = input.replace(x," %s " % x)
我得到以下输出:
I love programming with Python-3 . 3 ! Do you ? It's great . . . I give it a 10/10 . It's free-to-use , no $$$ involved !
它可以工作,但问题是它有时在标点符号和单词之间留下两个空格,例如句子中的第一个感叹号和单词“ Do”之间。 这是因为它们之间已经存在空间。
使用以下命令也会发生此问题:input =“ Hello。(hi)”。 输出为:
" Hello . ( hi ) "
注意左方括号前的两个空格。
除了上面提到的5个标点符号与单词没有分隔之外,我需要在任何标点符号和单词之间仅留一个空格的输出。 我怎样才能解决这个问题? 还是有更好的方法使用正则表达式来做到这一点?
提前致谢。
看起来re
可以为您做...
>>> import re
>>> re.sub(r"([\w/'+$\s-]+|[^\w/'+$\s-]+)\s*", r"\1 ", input)
"I love programming with Python-3 . 3 ! Do you ? It's great ... I give it a 10/10 . It's free- to-use , no $$$ involved ! "
和
>>> re.sub(r"([\w/'+$\s-]+|[^\w/'+$\s-]+)\s*", r"\1 ", "Hello. (hi)")
'Hello . ( hi ) '
如果尾随空格有问题, .rtrim(theresult, ' ')
应该为您解决:-)
我可以这样尝试吗:
>>> import string
>>> input = "I love programming with Python-3.3! Do you? It's great... I give it a 10/10. It's free-to-use, no $$$ involved!"
>>> ls = []
>>> for x in input:
... if x in string.punctuation:
... ls.append(' %s' % x)
... else:
... ls.append(x)
...
>>> ''.join(ls)
"I love programming with Python -3 .3 ! Do you ? It 's great . . . I give it a 10 /10 . It 's free -to -use , no $ $ $ involved !"
>>>
由于缺乏声誉而无法发表评论,但是在这种情况下,这里
在句子中的第一个感叹号和单词“ Do”之间
好像有两个空格,因为它们之间已经有一个空格了! 和做
! 做
因此,如果标点符号后已经有一个空格,请不要再放置其他空格。
另外,这里也有类似的问题: python regex在标点和字母之间插入空格
因此,也许考虑使用re
?
在我看来,否定的字符类更简单:
import re
input_string = "I love programming with Python-3.3! Do you? It's great... I give it a 10/10. It's free-to-use, no $$$ involved!"
print re.sub(r"\s?([^\w\s'/\-\+$]+)\s?", r" \1 ", input_string)
输出:
I love programming with Python-3 . 3 ! Do you ? It's great ... I give it a 10/10 . It's free-to-use , no $$$ involved !
# Approach 1
import re
sample_input = "I love programming with Python-3.3! Do you? It's great... I give it a 10/10. It's free-to-use, no $$$ involved!"
sample_input = re.sub(r"([^\s])([^\w\/'+$\s-])", r'\1 \2', sample_input)
print(re.sub(r"([^\w\/'+$\s-])([^\s])", r'\1 \2', sample_input))
# Approach 2
import string
sample_input = "I love programming with Python-3.3! Do you? It's great... I give it a 10/10. It's free-to-use, no $$$ involved!"
punctuation = string.punctuation.replace('/', '').replace("'", '') \
.replace('-', '').replace('+', '').replace('$', '')
i = 0
while i < len(sample_input):
if sample_input[i] not in punctuation:
i += 1
continue
if i > 0 and sample_input[i-1] != ' ':
sample_input = sample_input[:i] + ' ' + sample_input[i:]
i += 1
if i + 1 < len(sample_input) and sample_input[i+1] != ' ':
sample_input = sample_input[:i+1] + ' ' + sample_input[i+1:]
i += 1
i += 1
print(sample_input)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.