[Tutor] Creating a match file

Peter Otten __peter__ at web.de
Mon Sep 21 19:51:02 CEST 2015


EILEEN CHURCH CARSON wrote:

> I want to write a program that reads in data from two csv files with 6
> columns each. I want it to determines if the data in the first two columns
> is the same, and if so read all six columns in that line from each of the
> two files into a 12 column output file.
> 
> Please let me know if you know of specific resources or python commands
> that will help me do this.

Use the csv module to read and write your data.
Read one csv file into a dict that maps the two key columns to the complete 
row. Then when you read the other csv file you can look up the corresponding 
record in the dict. Here's a sketch of the part tha performs join*:

for row in ...:
    try:
        other = lookup[row[:2]]
    except KeyError:
        # no corresponding data found, ignore row
        pass 
    else:
        # write combined row
        # result_writer is a csv.writer() instance
        result_writer.writerow(row + other)

Come back with your code when you need more hints.

(*) If you know some SQL another option is to read the two csv files into an 
sqlite3 db -- sqlite3 is also part of the standard library.



More information about the Tutor mailing list