Changeset 5554 for workshop2010


Ignore:
Timestamp:
30 Sep 2010, 10:10:38 (14 years ago)
Author:
uli
Message:

Copy CSV file merger created last evening into repository.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • workshop2010/trunk/src/grokworkshop/mergecsv.py

    r5552 r5554  
    33import csv
    44
    5 input1 = """
    6 """
    7 
    8 input2 = """
    9 """
    10 
    11 
    12 def merge_csv_files():
     5def merge_csv_files(csv_path1=None, csv_path2=None,
     6                    wanted_headers=['id', 'student_id', 'name']):
    137    """Merge a file 'input1.csv' and 'input2.csv'.
    148    """
     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)
Note: See TracChangeset for help on using the changeset viewer.