python compare dict reader elements from two csv files

两盒软妹~` 提交于 2019-12-13 02:47:18

问题


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

回答1:


If you just want to find difference you can use difflib As an example: import difflib fo1 = open(csv) fo2 = open(CSV_new) diff =difflib.ndiff(fo1.readlines(),fo2.readlines()) Then you can write the difference as you want




回答2:


This could be what you are looking for, but as mentioned above there is some ambiguity in your description.

with open(A) as fd1, open(B) as fd2:
    a, b = csv.reader(fd1), csv.reader(fd2)
    ha, hb = next(a), next(b)
    if not set(ha).issubset(set(hb)):
        sys.exit(1)

    lookup = {label : (key, hb.index(label)) for key, label in enumerate(ha)}
    for rowa, rowb in zip(a, b):
        for key in lookup:
            index_a, index_b = lookup[key]
            if rowa[index_a] != rowb[index_b]:
                 print(rowb)
                 break


来源:https://stackoverflow.com/questions/28373625/python-compare-dict-reader-elements-from-two-csv-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!