parsing - python: how to delete row and modify specific list string from CSV -
this first time posting question apologise in advance if make mistakes.
i attempting create custom python program (pretty parser) takes in data 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 desired output is:
title title title data1 data2 data3 data4 data5 data6 data7 data8 data9
here working portion of code have far:
import csv import itertools open('file.log','rb') csvfile: rowlist = csv.reader(csvfile, delimiter = '\t') row in itertools.islice(rowlist,6,12): print row
whenever above code run produces series of lists seen here
['fields','title1', 'title2', 'title3'] ['data_type','d_type','d_type', 'd_type'] ['data1', 'data2', 'data3'] ['data4', 'data5', 'data6'] ['data7', 'data8', 'data9']
the first data entry (data1, data4, data7) in list number whereas other data entries may string/number/character.
the itertools
has solved cutting off top , bottom of file, still struggling to
- delete "data_type line"
- removing 'fields', is: ['fields','title1', 'title2', 'title3']----->['title1', 'title2', 'title3']
i have seen solutions remove lines/overwrite line however, not have lot of memory spare must keep opening/closing/writing minimum. , extremely appreciated.
just slice each row:
row in islice(rowlist, 6, 12): if row[0] == "data_type": continue elif row[0] == "fields": print(row[1:]) else: print(row)
if 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 trying overwrite original file, can write lines tempfile , replace original file shutil.move:
from shutil import move tempfile import namedtemporaryfile open('file.csv', 'rb') csvfile, namedtemporaryfile(dir=".", delete=false) temp: rowlist = csv.reader(csvfile) 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")
Comments
Post a Comment