Discussion:
[Tutor] Creating a match file
EILEEN CHURCH CARSON
2015-09-20 19:19:47 UTC
Permalink
Hi there,

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.

Thank you!

Eileen
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Peter Otten
2015-09-21 17:51:02 UTC
Permalink
Post by EILEEN CHURCH CARSON
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.

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Emile van Sebille
2015-09-21 22:50:02 UTC
Permalink
Post by EILEEN CHURCH CARSON
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.
I think I'd read in the first file and create a dictionary using the
first two fields as the key, and the remaining six fields as the data.
then, read in the second file, loop through again creating a key of the
first two fields and if that exists in the first dictionary, get that
data and write all 12 fields to a new dictionary. When done, write the
results to the output file.

Emile


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Continue reading on narkive:
Loading...