Search code examples
pythonsplitmarkdownline

Reading markdown file with elements separated by double line feeds


I have a markdown file (*.md) containing the following text:

Aliquam posuere

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec hendrerit 
tempor tellus. Donec pretium posuere tellus. Proin quam nisl, tincidunt et

BarPlot,bar.csv, Animal, Count, Animal Facts

I would like to read this markdown file and obtaining a list of 3 element that are, in the original *.md file, separated by an empty line. In other words, I'd like to get the following output:

result = [
    'Aliquam posuere', 
    'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec hendrerit tempor tellus. Donec pretium posuere tellus. Proin quam nisl, tincidunt et',
    'BarPlot,bar.csv, Animal, Count, Animal Facts'
]

with len(result) = 3.


Solution

  • Obviously you have to read in the file and split at a double linebreak (out of simplicity I take a string) like this:

    s = """Aliquam posuere
    
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec hendrerit
    tempor tellus. Donec pretium posuere tellus. Proin quam nisl, tincidunt et
    
    BarPlot,bar.csv, Animal, Count, Animal Facts
    
    """
    
    result = s.split('\n\n')
    result = result [:-1]
    

    You get a list of length 4 because you have a double linebreak at the end. Just remove the last item and you got your desired output:

    result = ['Aliquam posuere',
     'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec hendrerit\ntempor tellus. Donec pretium posuere tellus. Proin quam nisl, tincidunt et',
     'BarPlot,bar.csv, Animal, Count, Animal Facts']