问题
I need to separate texts into paragraphs and be able to work with each of them. How can I do that? Between every 2 paragraphs can be at least 1 empty line. Like this:
Hello world,
this is an example.
Let´s program something.
Creating new program.
Thanks in advance.
回答1:
This sould work:
text.split('\n\n')
回答2:
Try
result = list(filter(lambda x : x != '', text.split('\n\n')))
回答3:
I usually strip before split then filter out the ''. ;)
a =\
'''
Hello world,
this is an example.
Let´s program something.
Creating new program.
'''
data = [content for content in a.strip().splitlines() if content]
print(data)
回答4:
this is worked for me:
text = "".join(text.splitlines())
text.split('something that is almost always used to separate sentences (i.e. a period, question mark, etc.)')
来源:https://stackoverflow.com/questions/53240763/python-how-to-separate-paragraphs-from-text