python: how to delete row and modify specific list string from CSV
Question: This is my first time posting a question so I apologise in advance if I make
any mistakes.

I am currently attempting to create a custom python program (pretty much a
parser) that takes in data as such:

    
    
    junk
    junk
    junk
    junk
    junk
    junk
    fields       title   title   title
    data_type   d_type  d_type  d_type
    data1   data2    data 3
    data4   data 5   data6
    data7   data8    data9
    junk
    

Where my desired output is:

    
    
    title   title   title
    data1   data2   data3
    data4   data5   data6
    data7   data8   data9
    

Here is the working portion of my code that I have thus far:

    
    
    import csv
    import itertools
    with open('file.log','rb') as csvfile:
        rowlist = csv.reader(csvfile, delimiter = '\t')
        for row in itertools.islice(rowlist,6,12):   
        print row
    

Whenever the above code is run it produces a series of lists as seen here

    
    
    ['fields','title1', 'title2', 'title3']
    ['data_type','d_type','d_type', 'd_type']
    ['data1', 'data2', 'data3']
    ['data4', 'data5', 'data6']
    ['data7', 'data8', 'data9']
    

The the first data entry (data1, data4, data7) in the list is always a number
whereas the other data entries may be any string/number/character.

The `itertools` has solved cutting off the top and bottom of the file, however
I am still struggling to

  * delete the "data_type line" 
  * removing 'fields', that is: ['fields','title1', 'title2', 'title3']----->['title1', 'title2', 'title3']

I have seen some solutions that remove lines/overwrite line however, I do not
have a lot of memory to spare thus I must keep opening/closing/writing to a
minimum. Any and all help is extremely appreciated.


Answer: Just slice each row:

    
    
      for row in islice(rowlist, 6, 12):
        if row[0] == "data_type":
            continue
        elif row[0] == "fields":
              print(row[1:])
        else:
            print(row)
    

If you are just writing rows use islice again:

    
    
    for row in islice(rowlist, 6, 12):
        if row[0] == "data_type":
            continue
        elif row[0] == "fields":
             fileobj.write(islice(rowlist, 1,None))
        else:
            fileobj.write(row)
    

If you are actually trying to overwrite the original file, you can write the
lines to a
[tempfile](https://docs.python.org/2/library/tempfile.html#tempfile.NamedTemporaryFile)
and replace the original file with
[shutil.move](https://docs.python.org/2/library/shutil.html#shutil.move):

    
    
    from shutil import move
    from tempfile import NamedTemporaryFile
    
    with open('file.csv', 'rb') as csvfile, NamedTemporaryFile(dir=".", delete=False) as temp:
        rowlist = csv.reader(csvfile)
        for row in islice(rowlist, 6, 12):
            if row[0] == "data_type":
                continue
            elif row[0] == "fields":
                temp.write(islice(rowlist, 1, None))
            else:
                temp.write(row)
    
    move(temp.name,"file.csv")
    

