簡體   English   中英

Python-如何通過空格將標點符號與單詞分開,在標點符號和單詞之間僅留一個空格?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM