Reduzieren Sie X Newlines mit Regex in X-1-NewlinesPython

Python-Programme
Anonymous
 Reduzieren Sie X Newlines mit Regex in X-1-Newlines

Post by Anonymous »

Ich habe diesen Text:

Code: Select all

""Anna lives in Latin America.\n\nShe loves the vibes from the cities\n and the good weather.\n\n\nAnna is great"

und ich möchte die X-Newlines in X-1-Newlines reduzieren. Das erwartete Ergebnis ist also:

Code: Select all

"Anna lives in Latin America.\nShe loves the vibes from the cities and the good weather.\n\nAnna is great"

Ich habe dies ausprobiert:

Code: Select all

import re

def clean_text(text):
text = re.sub(r'\n{2,}', '\n', text)
return text
result = clean_text("Anna lives in Latin America.\n\nShe loves the vibes from the cities\n and the good weather.\n\n\nAnna is great")
result
Aber es gibt zurück "Anna lebt in Lateinamerika. \ nshe liebt die Stimmung aus den Städten \ n und das gute Wetter. \ nanna ist großartig" , was ich nicht erwartet hatte.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post