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
 Mobile version
 Mobile version