python - Fix numbering on CSV files that have deleted lines -
i have bunch of csv files have edited , gotten rid of of lines have 'dif' in them. problem realized later count number in file stays same before. here example of csv before edit it.
name bunch of stuff header stuff stuff header stuff stuff header stuff stuff header stuff stuff header stuff stuff count 11 number,item n1,shoe n2,heel n3,tee n4,polo n5,sneaker n6,dif n7,dif n8,dif n9,dif n10,heel n11,tee
this how output csv looks. want number next 'count' equal number in 'items' column have in 'number' column sequential.
name bunch of stuff header stuff stuff header stuff stuff header stuff stuff header stuff stuff header stuff stuff count 11 number,item n1,shoe n2,heel n3,tee n4,polo n5,sneaker n10,heel n11,tee
here current code that. want to, screws rest of csv mentioned above.
import csv import glob import os fns = glob.glob('*.csv') #goes through every csv file in directory fn in fns: reader=csv.reader(open(fn,"rb")) open (os.path.join('out', fn), 'wb') f: w = csv.writer(f) row in reader: if not ' dif' in row: #remove dif w.writerow(row)
i've tried few small things fix it, new programming , nothing try seems much. appreciated.
thank you
if need update count, have read twice , count number of rows keeping first. can keep separate counter rewrite first column once writing matched lines:
import re numbered = re.compile(r'n\d+').match fn in fns: # open counting reader = csv.reader(open(fn,"rb")) count = sum(1 row in reader if row , not any(r.strip() == 'dif' r in row) , numbered(row[0])) # reopen filtering reader = csv.reader(open(fn,"rb")) open (os.path.join('out', fn), 'wb') f: counter = 0 w = csv.writer(f) row in reader: if row , 'count' in row[0].strip(): row = ['count', count] if row , not any(r.strip() == 'dif' r in row): #remove dif if numbered(row[0]): counter += 1 row[0] = 'n%d' % counter w.writerow(row)
Comments
Post a Comment