1 | """Merge two CSV files and output the results. |
---|
2 | """ |
---|
3 | import csv |
---|
4 | |
---|
5 | def merge_csv_files(csv_path1=None, csv_path2=None, |
---|
6 | wanted_headers=['id', 'student_id', 'name']): |
---|
7 | """Merge a file 'input1.csv' and 'input2.csv'. |
---|
8 | """ |
---|
9 | input1 = csv_path1 |
---|
10 | input2 = csv_path2 |
---|
11 | |
---|
12 | students_reader = csv.DictReader(open(input1, 'rb')) |
---|
13 | payments_reader = csv.DictReader(open(input2, 'rb')) |
---|
14 | |
---|
15 | result_writer = None |
---|
16 | |
---|
17 | students = dict() |
---|
18 | for student in students_reader: |
---|
19 | # Read all students and store them under their respective id |
---|
20 | students[student['id']] = student |
---|
21 | for payment in payments_reader: |
---|
22 | # Read all payments and lookup a matching student |
---|
23 | student_id = payment['student_id'] |
---|
24 | |
---|
25 | if student_id not in students.keys(): |
---|
26 | # Oops, what happened? |
---|
27 | print "STUDENT NOT FOUND: ", student_id |
---|
28 | continue |
---|
29 | |
---|
30 | student = students[student_id] |
---|
31 | result_line = payment |
---|
32 | result_line.update(student) |
---|
33 | if result_writer is None: |
---|
34 | # Create a CSV writer and ouput header line |
---|
35 | result_writer = csv.DictWriter(open('result.csv', 'wb'), |
---|
36 | wanted_headers, |
---|
37 | extrasaction='ignore') |
---|
38 | header_line = dict([(x, x) for x in wanted_headers]) |
---|
39 | result_writer.writerow(header_line) |
---|
40 | result_writer.writerow(result_line) |
---|