I have two CSV files that I'm trying to compare. I've read them using dict reader. So now I have dictionaries (one for each row) from two CSV files. I want to compare them, say when two elements (those with headers h1 and h2) are same, compare those dictionaries and print out the differences with respect to the second dictionary. Here are sample csv files.
csv1:
h1,h2,h3
aaa,g0,74
bjg,73,kg9
CSV_new:
h1,h2,h3,h4
aaa,g0,7,
bjg,73,kg9,ahf
I want the output to be something like this, though not exactly like shown below, I want it to be able to print out the modifications, additions and deletions in each dictionary with respect to CSV_new:
{h1:'aaa', h2:'g0' {h3:'74', h4:''}}
{h1:'bjg', h2:'73' {h4:''}
My code, that's not well-developed enough.
import csv
f1 = "csv1.csv"
reader1 = csv.DictReader(open (f1), delimiter = ",")
for row1 in reader1:
row1['h1']
#['%s:%s' % (f, row[f]) for f in reader.fieldnames]
f2 = "CSV_new.csv"
reader2 = csv.DictReader(open (f2), delimiter = ",")
for row2 in reader2:
row2['h1']
if row1['h1'] == row2['h1']:
print row1, row2
csv.DictReaderobjects is a dictionary variable with a name (row1orrow2). If you stored them in lists, their variable names would become something likelist_name[i]whereiis an integer variable (or integer constant). - martineau