Advertisement
DeaD_EyE

agregate / csv / itertools

Jun 23rd, 2019
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.72 KB | None | 0 0
  1. import csv
  2. from itertools import chain, groupby
  3.  
  4.  
  5. def agregate(*csv_files, output_file):
  6.     grouping = lambda row: row[1]
  7.     fds = [open(file) for file in csv_files]
  8.     csv_files = (csv.reader(f, delimiter=';') for f in fds)
  9.     csv_data = sorted(chain.from_iterable(csv_files), key=grouping)
  10.     [file.close() for file in fds]
  11.     with open(output_file, 'w') as fd:
  12.         writer = csv.writer(fd, delimiter=';')
  13.         for group, elements in groupby(csv_data, key=grouping):
  14.             produkte = []
  15.             for produkt, e_mail, vorname, nachname in elements:
  16.                 produkte.append(produkt)
  17.             produkte = '\n'.join(produkte)
  18.             writer.writerow((produkte, e_mail, vorname, nachname))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement