/ / Python - 句読点と単語の間に空白を1つだけ残して、句読点を空白文字で区切るにはどうすればよいですか? - Python、正規表現

Python - 句読点と単語の間にスペースを1つだけ残して、句読点を空白文字で区切るにはどうすればよいですか? - Python、正規表現

私は次の文字列を持っています:

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"の間など、句読点と単語の間に2つのスペースがあります。これは、それらの間に既にスペースがあるためです。

この問題はinput = "Hello。(hi)"でも発生します。出力は次のようになります。

" Hello .  ( hi ) "

開いた金具の前の2つのスペースに注意してください。

私は出力を必要とします。句読点と言葉は、上記の5つの句読点を除いて、単語から分離されていません。これをどうすれば解決できますか?または、正規表現を使用してこれを行うより良い方法はありますか?

前もって感謝します。

回答:

回答№1の場合は3

見える 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, " ") あなたのためにそれを修正する必要があります:-)


回答№2の場合は0

私はこのように試すことができます:

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

回答№3の場合は0

評判の不足によりコメントできませんでしたが、この場合はここで

文中の最初の感嘆符と単語 "Do"の間には、

間にスペースがあるので、2つのスペースがあるように見えます!そして、やります

! 行う

だから、すでに句読点の後にスペースがある場合は、別のスペースを入れないでください。

また、ここにも同様の質問があります: Python正規表現と文字の間にスペースを挿入する正規表現

だから、 re


回答№4の場合は0

それは私には否定された文字クラスは簡単だと思われる:

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?([^ws"/-+$]+)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 !

回答№5の場合は0
# 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)