python - Filling a row in a csv file according -
here csv file want update python. has 3 columns:
col1 = [1,2,3,4,5,6,7,8,9,10] col2 = [a, a, a, b, c, c, c, c, a, a] col3 = []
for each cell in col3 fill id number change when content of col2 changes.
in example col3 = [1,1,1,2,3,3,3,3,4,4]
anyone show me way ?
in python 3:
from itertools import groupby operator import itemgetter import csv open('f1.csv', newline='') f_in, open('f2.csv', 'w', newline='') f_out: reader = csv.reader(f_in) writer = csv.writer(f_out) i, (key, group) in enumerate(groupby(reader, key=itemgetter(1)), start=1): row in group: writer.writerow(row + [i])
if using python 2.7, change with-statement to
with open('f1.csv', 'rb') f_in, open('f2.csv', 'wb') f_out:
Comments
Post a Comment