Python - how to separate paragraphs from text?











up vote
0
down vote

favorite












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.










share|improve this question









New contributor




kom20 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Assuming the text is in a text file. Read the file line wise and whenever you encounter a blank line, you know that whatever was above that line belonged to a paragraph. Extend this similarly for upcoming text.
    – raj
    Nov 10 at 16:10










  • This is clear for me, but I need a help with syntax, how to write this.
    – kom20
    Nov 10 at 16:14






  • 1




    @kom20 do you know how to open a file and read a line? What difficultly do you have specifically ?
    – Jon Clements
    Nov 10 at 16:17










  • I know this, but I need to align all paragraphs for set width of characters and for that I need to separate paragraphs from the text and work with each individually.
    – kom20
    Nov 10 at 16:21










  • Use str.splitlines()
    – snakecharmerb
    Nov 10 at 16:30















up vote
0
down vote

favorite












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.










share|improve this question









New contributor




kom20 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Assuming the text is in a text file. Read the file line wise and whenever you encounter a blank line, you know that whatever was above that line belonged to a paragraph. Extend this similarly for upcoming text.
    – raj
    Nov 10 at 16:10










  • This is clear for me, but I need a help with syntax, how to write this.
    – kom20
    Nov 10 at 16:14






  • 1




    @kom20 do you know how to open a file and read a line? What difficultly do you have specifically ?
    – Jon Clements
    Nov 10 at 16:17










  • I know this, but I need to align all paragraphs for set width of characters and for that I need to separate paragraphs from the text and work with each individually.
    – kom20
    Nov 10 at 16:21










  • Use str.splitlines()
    – snakecharmerb
    Nov 10 at 16:30













up vote
0
down vote

favorite









up vote
0
down vote

favorite











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.










share|improve this question









New contributor




kom20 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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.







python paragraph






share|improve this question









New contributor




kom20 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




kom20 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Nov 10 at 21:02









user6910411

31.6k76592




31.6k76592






New contributor




kom20 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 10 at 16:02









kom20

6




6




New contributor




kom20 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





kom20 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






kom20 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • Assuming the text is in a text file. Read the file line wise and whenever you encounter a blank line, you know that whatever was above that line belonged to a paragraph. Extend this similarly for upcoming text.
    – raj
    Nov 10 at 16:10










  • This is clear for me, but I need a help with syntax, how to write this.
    – kom20
    Nov 10 at 16:14






  • 1




    @kom20 do you know how to open a file and read a line? What difficultly do you have specifically ?
    – Jon Clements
    Nov 10 at 16:17










  • I know this, but I need to align all paragraphs for set width of characters and for that I need to separate paragraphs from the text and work with each individually.
    – kom20
    Nov 10 at 16:21










  • Use str.splitlines()
    – snakecharmerb
    Nov 10 at 16:30


















  • Assuming the text is in a text file. Read the file line wise and whenever you encounter a blank line, you know that whatever was above that line belonged to a paragraph. Extend this similarly for upcoming text.
    – raj
    Nov 10 at 16:10










  • This is clear for me, but I need a help with syntax, how to write this.
    – kom20
    Nov 10 at 16:14






  • 1




    @kom20 do you know how to open a file and read a line? What difficultly do you have specifically ?
    – Jon Clements
    Nov 10 at 16:17










  • I know this, but I need to align all paragraphs for set width of characters and for that I need to separate paragraphs from the text and work with each individually.
    – kom20
    Nov 10 at 16:21










  • Use str.splitlines()
    – snakecharmerb
    Nov 10 at 16:30
















Assuming the text is in a text file. Read the file line wise and whenever you encounter a blank line, you know that whatever was above that line belonged to a paragraph. Extend this similarly for upcoming text.
– raj
Nov 10 at 16:10




Assuming the text is in a text file. Read the file line wise and whenever you encounter a blank line, you know that whatever was above that line belonged to a paragraph. Extend this similarly for upcoming text.
– raj
Nov 10 at 16:10












This is clear for me, but I need a help with syntax, how to write this.
– kom20
Nov 10 at 16:14




This is clear for me, but I need a help with syntax, how to write this.
– kom20
Nov 10 at 16:14




1




1




@kom20 do you know how to open a file and read a line? What difficultly do you have specifically ?
– Jon Clements
Nov 10 at 16:17




@kom20 do you know how to open a file and read a line? What difficultly do you have specifically ?
– Jon Clements
Nov 10 at 16:17












I know this, but I need to align all paragraphs for set width of characters and for that I need to separate paragraphs from the text and work with each individually.
– kom20
Nov 10 at 16:21




I know this, but I need to align all paragraphs for set width of characters and for that I need to separate paragraphs from the text and work with each individually.
– kom20
Nov 10 at 16:21












Use str.splitlines()
– snakecharmerb
Nov 10 at 16:30




Use str.splitlines()
– snakecharmerb
Nov 10 at 16:30












3 Answers
3






active

oldest

votes

















up vote
1
down vote













This sould work:



text.split('nn')





share|improve this answer





















  • Thanks, it seems good. But since the end of the text consists of some empty lines, last items in this list are empty (like this): ["something","",""]. Can this make any problem as soon as I get into work with the particular words in these paragraphs?
    – kom20
    Nov 10 at 18:00












  • This is for you to say. You can always filter them out with filter(None, ...)
    – roeen30
    Nov 10 at 18:26




















up vote
0
down vote













Try



result = list(filter(lambda x : x != '', text.split('nn')))





share|improve this answer























  • While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer.
    – hellow
    Nov 11 at 7:21


















up vote
0
down vote













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)





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });






    kom20 is a new contributor. Be nice, and check out our Code of Conduct.










     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240763%2fpython-how-to-separate-paragraphs-from-text%23new-answer', 'question_page');
    }
    );

    Post as a guest
































    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    This sould work:



    text.split('nn')





    share|improve this answer





















    • Thanks, it seems good. But since the end of the text consists of some empty lines, last items in this list are empty (like this): ["something","",""]. Can this make any problem as soon as I get into work with the particular words in these paragraphs?
      – kom20
      Nov 10 at 18:00












    • This is for you to say. You can always filter them out with filter(None, ...)
      – roeen30
      Nov 10 at 18:26

















    up vote
    1
    down vote













    This sould work:



    text.split('nn')





    share|improve this answer





















    • Thanks, it seems good. But since the end of the text consists of some empty lines, last items in this list are empty (like this): ["something","",""]. Can this make any problem as soon as I get into work with the particular words in these paragraphs?
      – kom20
      Nov 10 at 18:00












    • This is for you to say. You can always filter them out with filter(None, ...)
      – roeen30
      Nov 10 at 18:26















    up vote
    1
    down vote










    up vote
    1
    down vote









    This sould work:



    text.split('nn')





    share|improve this answer












    This sould work:



    text.split('nn')






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 10 at 17:00









    roeen30

    20419




    20419












    • Thanks, it seems good. But since the end of the text consists of some empty lines, last items in this list are empty (like this): ["something","",""]. Can this make any problem as soon as I get into work with the particular words in these paragraphs?
      – kom20
      Nov 10 at 18:00












    • This is for you to say. You can always filter them out with filter(None, ...)
      – roeen30
      Nov 10 at 18:26




















    • Thanks, it seems good. But since the end of the text consists of some empty lines, last items in this list are empty (like this): ["something","",""]. Can this make any problem as soon as I get into work with the particular words in these paragraphs?
      – kom20
      Nov 10 at 18:00












    • This is for you to say. You can always filter them out with filter(None, ...)
      – roeen30
      Nov 10 at 18:26


















    Thanks, it seems good. But since the end of the text consists of some empty lines, last items in this list are empty (like this): ["something","",""]. Can this make any problem as soon as I get into work with the particular words in these paragraphs?
    – kom20
    Nov 10 at 18:00






    Thanks, it seems good. But since the end of the text consists of some empty lines, last items in this list are empty (like this): ["something","",""]. Can this make any problem as soon as I get into work with the particular words in these paragraphs?
    – kom20
    Nov 10 at 18:00














    This is for you to say. You can always filter them out with filter(None, ...)
    – roeen30
    Nov 10 at 18:26






    This is for you to say. You can always filter them out with filter(None, ...)
    – roeen30
    Nov 10 at 18:26














    up vote
    0
    down vote













    Try



    result = list(filter(lambda x : x != '', text.split('nn')))





    share|improve this answer























    • While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer.
      – hellow
      Nov 11 at 7:21















    up vote
    0
    down vote













    Try



    result = list(filter(lambda x : x != '', text.split('nn')))





    share|improve this answer























    • While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer.
      – hellow
      Nov 11 at 7:21













    up vote
    0
    down vote










    up vote
    0
    down vote









    Try



    result = list(filter(lambda x : x != '', text.split('nn')))





    share|improve this answer














    Try



    result = list(filter(lambda x : x != '', text.split('nn')))






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 10 at 21:14

























    answered Nov 10 at 21:05









    bolzano

    3621827




    3621827












    • While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer.
      – hellow
      Nov 11 at 7:21


















    • While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer.
      – hellow
      Nov 11 at 7:21
















    While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer.
    – hellow
    Nov 11 at 7:21




    While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer.
    – hellow
    Nov 11 at 7:21










    up vote
    0
    down vote













    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)





    share|improve this answer

























      up vote
      0
      down vote













      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)





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        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)





        share|improve this answer












        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)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 10 at 21:25









        Prayson Daniel

        1,0381817




        1,0381817






















            kom20 is a new contributor. Be nice, and check out our Code of Conduct.










             

            draft saved


            draft discarded


















            kom20 is a new contributor. Be nice, and check out our Code of Conduct.













            kom20 is a new contributor. Be nice, and check out our Code of Conduct.












            kom20 is a new contributor. Be nice, and check out our Code of Conduct.















             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240763%2fpython-how-to-separate-paragraphs-from-text%23new-answer', 'question_page');
            }
            );

            Post as a guest




















































































            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine