kristjanleifur
kristjanleifur

Reputation: 33

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

Upvotes: 3

Views: 1956

Answers (2)

Jorge Orozco-Sanchez
Jorge Orozco-Sanchez

Reputation: 89

You must first concatenate the lines of a paragraph:

def make_list_from_file(file_stream):
    paragraph_list = []
    temp_paragraph = ""
    for line in file_stream:
        temp_paragraph += line + "<separator between lines>"
        if(line == "<NEW DOCUMENT>")
            paragraph_list.append(temp_paragraph)
    return paragraph_list

Upvotes: 0

IoaTzimas
IoaTzimas

Reputation: 10624

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.

Upvotes: 3

Related Questions