Extracting multiple lines of strings
<h2>Update:</h2>

<p>I spoke to a colleague about this and we came up with the following solution...</p>

<pre><code>#!/usr/bin/python python

in_file = open("small-output.txt", "rt")

with open("output-new.txt", "wt") as txtfile:
    sentence = ""
    hit = False
    for each in in_file:
        if each.strip() == "Description":
            hit = False
            txtfile.write(sentence + "\n")
            sentence = ""
        if hit == True: sentence += " " + each.strip()
        if each.strip() == "Title": hit = True
txtfile.close()
in_file.close()
</code></pre>

<p>It's not a perfect/elegant solution in that there are problems writing out to a .csv file with all the inline commas. So, what I ended up doing was just writing out to a text file using the script above and then importing that into a .csv.</p>

<p>Ideally, the output would look like </p>

<blockquote>
  <p>This is the title of the thing, also foo.</p>
</blockquote>

<p>With that in mind, can anyone improve the code so that each captured sentence is a row in a spreadsheet? Or does anyone have a more elegant solution using Python 2.7?</p>

<h2>End update:</h2>

<p>I have been looking through StackExchange all morning and, while I have seen many similar solutions, I have yet to find one that exactly fits my parameters. I am attempting to write a script that parses a text file, copies multiple lines of text between two delimiters, and then pastes each string set into a .csv file. The lines in the text file look like:</p>

<pre><code>a string
    ......
    a string
    another string, a string
    Title
    This
    is
    the
    title
    of
    the
    thing, 
    also foo.
    Description
    a string
    ..........
    another string
</code></pre>

<p>Specifically, I am looking to capture everything between 'Title' and 'Description', then write it out to a .csv file.  </p>

<p>This started off as a very large PDF (10,000+ pages) that has been exported to a text file using pdfminer and there are lots of instances of the delimeters; so, ideally, the output would be lots of rows of cells with some sentences in them.</p>

<p>So far, I have used Python 2.7 and regular expressions, but am open to other *nix methods, e.g. awk, sed, grep, etc. </p>

<p>Here are some of the non-working snippets I have tried...</p>

<pre><code>#!/usr/bin/python python
import re, csv

text_file = open('test_file.txt')

with open(text_file, 'wb') as fout:
    for result in re.findall('Description(.*?)Family', enb_document.read(), re.S):
             # fout.write(result)
fout.close()
</code></pre>

<hr>

<pre><code>def extractData():
    filename = ("test_file.txt")
    infile = open(filename,'r')
    startdelim = 'Description'
    enddelim = 'Family'

    for x in infile.readlines():
        x = x.strip()
        if x.startswith(startdelim):
            print &gt;&gt; sequence
        else:
            sequence = x
            if delim1.startswith(enddelim):



    infile.close()

extractData()
</code></pre>

<p>Any ideas? Thanks in advance!</p>

<pre><code>sed -n '/^Title$/,/^Description$/{//d;p}' file
</code></pre>

<p>outputs</p>

<pre><code>This
is
the
title
of
the
thing.
</code></pre>

python csv awk sed