Python - how to separate paragraphs from text?

只谈情不闲聊 提交于 2020-07-31 03:22:08

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!