Q:

Extract part of the data from a csv file using python

Problem
Extract a block of data from a file.
Description
Python reads a file. If it finds a particular string, it starts copying  all the next lines into an output file (included this first line), until is 'switched off'. The key-string is only in the first line.
An attempt
with open ('messy.csv', 'rt') as filein, open('nice.csv', 'w') as fileout:  
    for line in filein:
      if 'string' in line:  
        start=0
        while start<20:
          fileout.write(line)
          start+=1                    

The problem of this code, of course, is that the line copied isn't actualized (it just repeats the first line). Would you help me to solve it? (don't actually need the complete solution)

A:

Does this work for you?
with open ('messy.csv', 'rt') as filein, open('nice.csv', 'w') as fileout:  
    copy_flag = False
    start=0
    for line in filein:
        if 'string' in line:  
            copy_flag = True
        if copy_flag and start < 20:
            fileout.write(line)
            start+=1  

