Вы, кажется, пытаетесь сгруппировать ваш текст в два (или более) блока, разделенных двойными символами новой строки.Один из таких подходов - сначала разбить текст на \n\n
.Это приведет к blocks
, которые все еще содержат одиночные переводы строки.Каждый блок может затем заменить любые оставшиеся символы новой строки пробелами.Все это можно сделать, используя понимание списка Python следующим образом:
text = """Facebook and Google exploited a feature
intended for “enterprise developers” to
distribute apps that collect large amounts
of data on private users, TechCrunch first reported.
Apple’s maneuver has been characterized by some as a chilling demonstration of the company’s power.
Verge editor-in-chief Nilay Patel suggested in a tweet that it was cause for concern: First, they came for our enterprise certificates, then… well, what, exactly?"""
content = [block.replace('\n', ' ') for block in text.split('\n\n')]
print(content)
Предоставление вам списка с двумя записями и без перевода строки:
['Facebook and Google exploited a feature intended for “enterprise developers” to distribute apps that collect large amounts of data on private users, TechCrunch first reported.', 'Apple’s maneuver has been characterized by some as a chilling demonstration of the company’s power. Verge editor-in-chief Nilay Patel suggested in a tweet that it was cause for concern: First, they came for our enterprise certificates, then… well, what, exactly?']
Можно использовать регулярное выражениеиспользуется для случая, когда блоки разделены двумя или более пустыми строками следующим образом:
import re
text = """Facebook and Google exploited a feature
intended for “enterprise developers” to
distribute apps that collect large amounts
of data on private users, TechCrunch first reported.
Apple’s maneuver has been characterized by some as a chilling demonstration of the company’s power.
Verge editor-in-chief Nilay Patel suggested in a tweet that it was cause for concern: First, they came for our enterprise certificates, then… well, what, exactly?"""
content = [block.replace('\n', ' ') for block in re.split('\n{2,}', text)]
print(content)