Search code examples
pythonlistlineparagraph

How to add paragraphs to list in Python


I want to have user input a filename which contains a couple of paragraphs that are seperated by the string '<NEW DOCUMENT>'. Example text file:

Look on the bright 
side of Life.
<NEW DOCUMENT>
look on the very, dark
side of the Moon
<NEW DOCUMENT>
is there life
on the moon

I want to read this file and split it so that each index in the list has multiple lines For example if I print: print(paragraph_list[0]), the output would be the first paragraph (text file can contain more than two lines in each paragraph)

What I have tried is:

def make_list_from_file(file_stream):
    paragraph_list = []
    for line in file_stream:
        line.strip().split('<NEW DOCUMENT>')
        paragraph_list.append(line)
    return paragraph_list

I have tried other combinations but when I print the first index the output is just the first line in the text file

Look on the bright

Solution

  • The following should work:

    def make_list_from_file(file_stream):
        with open(file_stream) as f:
            t=f.read()
        paragraph_list=t.split('<NEW DOCUMENT>')
        return paragraph_list
    

    If you want to print some paragraph, result will be:

    >>> print(paragraph_list[0])
    Look on the bright
    side of Life.