#!/usr/bin/env python3
import argparse

def load_file(filename, data):
	with open(filename, 'r') as infile:
		for line in infile:
			line = line.strip()
			try:
				l = line.split(';')
				count = int(l[-1])
				t = ';'.join(l[:-1])
				if t in data:
					data[t] += count
				else:
					data[t] = count
			except:
				print("Wrong line:'" + line + "', skipping")

def main():
	parser = argparse.ArgumentParser(description='merge_files.py -- A script for merging and accumulating multiple files with dictionaries or tuples.')
	parser.add_argument('--input', action='append', required=True, help='Input files.')
	parser.add_argument('--outfile', action='store',required=True, help='Output file.')

	args = parser.parse_args()

	data = {}
	for filename in args.input:
		load_file(filename, data)

	with open(args.outfile, 'w') as outfile:
		for t in data:
			outfile.write(t + ';' + str(data[t]) + '\n')
			

if __name__ == '__main__':
	main()



