SHARE
TWEET

agregate / csv / itertools

DeaD_EyE Jun 23rd, 2019 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top