Python Extract Text between two strings into Excel
<p>I have a text file like this</p>

<pre><code>blablablabla
blablablabla
Start
Hello
World
End
blablabla
</code></pre>

<p>I want to extract the strings between Start and End and write them into an Excel cell. My code thus far looks like this:</p>

<pre><code>import xlsxwriter
workbook = xlsxwriter.Workbook("Test1.xlsx")
worksheet = workbook.add_worksheet()

flist = open("TextTest.txt").readlines()

parsing = False
for line in flist:

    if line.startswith("End"):
       parsing = False
    if parsing:
       worksheet.write(1,1,line)
    if line.startswith("Start"):
       parsing = True

workbook.close()
</code></pre>

<p>However it returns only an empty workbook. What am I doing wrong?</p>

<p>First of all, you seem to be always writing in the line number 1</p>

<p>Second, the numeration starts at 0</p>

<p>With these two small changes, this should do what you want :</p>

<pre><code>parsing = False
linewrite=0

for line in liste:

    if line.startswith("End"):
       parsing = False
    if parsing:
       worksheet.write(linewrite,0,line)
       print line,
       linewrite+=1
    if line.startswith("Start"):
       parsing = True

workbook.close()
</code></pre>

python excel text