Parsing paragraph out of text file in Python?
<p>I am trying to parse certain paragraphs out of multiple text file and store them in list. All the text file have some similar format to this:</p>

<pre><code>MODEL NUMBER: A123

MODEL INFORMATION: some info about the model

DESCRIPTION: This will be a description of the Model. It 
could be multiple lines but an empty line at the end of each.

CONCLUSION: Sold a lot really profitable.
</code></pre>

<p>Now i can pull out the information where its one line, but am having trouble when i encounter something which is multiple line (like 'Description'). The description length is not known but i know at the end it would have an empty line (which would mean using '\n'). This is what i have so far:</p>

<pre><code>import os
dir = 'Test'
DESCRIPTION = []
for files in os.listdir(dir):
    if files.endswith('.txt'): 
        with open(dir + '/' + files) as File:
            reading = File.readlines()
            for num, line in enumerate(reading):
                if 'DESCRIPTION:' in line:
                    Start_line = num
                if len(line.strip()) == 0:
</code></pre>

<p>I don't know if its the best approach, but what i was trying to do with <code>if len(line.strip()) == 0:</code> is to create a list of blank lines and then find the first greater value than <code>Start_Line</code>. I saw this <a href="http://stackoverflow.com/questions/7281760/in-python-how-do-you-find-the-index-of-the-first-value-greater-than-a-threshold">Bisect</a>.</p>

<p>In the end i would like my data to be if i say <code>print Description</code></p>

<pre><code>['DESCRIPTION: Description from file 1', 
'DESCRIPTION: Description from file 2', 
'DESCRIPTION: Description from file 3,]
</code></pre>

<p>Thanks.</p>

<p>Regular expression. Think about it this way: you have a pattern that will allow you to cut any file into pieces you will find palatable: "newline followed by capital letter"</p>

<p>re.split is your friend</p>

<p>Take a string</p>

<pre><code>"THE
BEST things 
in life are
free
IS
YET
TO
COME"
</code></pre>

<p>As a string:</p>

<pre><code>p = "THE\nBEST things\nin life are\nfree\nIS\nYET\nTO\nCOME"
c = re.split('\n(?=[A-Z])', p)
</code></pre>

<p>Which produces list c </p>

<p><code>['THE', 'BEST things\nin life are\nfree', 'IS', 'YET', 'TO', 'COME']</code></p>

<p>I think you can take it from there, as this would separate your files into each a list of strings with each string beings its own section, then from there you can find the "DESCRIPTION" element and store it, you see that you separate each section, including its subcontents by that re split. Important to note that the way I've set up the regex it recognies the PATTERN "newline and then Capital Letter" but CUTS after the newline, which is why it is outside the brackets. </p>

python list python-2.7 file