Python CSV Comparison
Question: This script compares two csv files...with two columns plz help me to modify
this script if sample1.csv and sample2.csv has more than 2 columns or 1
column.

    
    
    f1_in = open("sample1.csv","r")
    next(f1_in,None)
    f1_dict = {}
    for line in f1_in:
      l = line.split(',')
      f1_dict[l[0]. strip()] = l[1]. strip() 
      l.sort()
    f1_in.close()
    
    f2_in = open("sample2.csv","r")
    next(f2_in,None)
    f2_dict = {}
    for line in f2_in:
      l = line.split(',')
      f2_dict[l[0]. strip()] = l[1]. strip()
      l.sort()
    f2_in.close()
    
    
    f_same = open("same.txt","w")
    f_different = open("different.txt","w")
    
    for k1 in f1_dict.keys():
      if k1 in f2_dict.keys() \
          and f2_dict[k1] == f1_dict[k1]:
        f_same.write("{0}, {1}\n". format(str(k1)+" "+str(f1_dict[k1]),
                                        str(k1)+" "+str(f2_dict[k1])))
    
      elif not k1 in f2_dict.keys():
        f_different.write("{0}, {1}\n". format(str(k1)+" "+str(f1_dict[k1]),
                                               "------"))
      elif not f2_dict[k1] == f1_dict[k1]:
        f_different.write("{0}, {1}\n". format(str(k1)+" "+str(f1_dict[k1]),
                                               str(k1)+" "+str(f2_dict[k1])))
    
    f_same.close()
    f_different.close()
    

for eg:if my source file has Name and Salary as headers with values A 20000 B
15000 C 10000 D 10000 and target file also with Name and Salary has headers
with values A 40000 D 10000 B 15000 C 10000 E 8000...my output should be
Different lines:A 20000 A 40000 D 10000 -----(no file in target) -----(no file
in source) E 8000 and common lines as B 15000 B 15000, C 10000 C 10000


Answer: It is no wonder you cannot expand the code to more than two colums if you are
regarding your columns as key/value pairs in a dictionary.

You have to see them as "elements in a set". I understand this is why you are
not using the `csv` module or the `difflib` module: because you do not care
whether the lines appear in (nearly) same order in either file, but whether
they appear at all.

Here is an example:

    
    
    import itertools
    
    
    def compare(first_filename, second_filename):
        lines1 = set()
        lines2 = set()
        with open(first_filename, 'r') as file1, \
                open(second_filename, 'r') as file2:
            for line1, line2 in itertools.izip_longest(file1, file2):
                if line1:
                    lines1.add(line1)
                if line2:
                    lines2.add(line2)
        print "Different lines"
        for line in lines1 ^ lines2:
            print line,
        print "---"
        print "Common lines"
        for line in lines1 & lines2:
            print line,
    

Notice that this code will find differences on both files, not just things
that exist on f1 but not on f2, as your example does. However, it is not able
to tell where do differences come from (since this does not seem a requirement
of the question).

### Check that it works

    
    
    In [40]: !cat sample1.csv
    bacon, eggs, mortar
    whatever, however, whenever
    spam, spam, spam
    
    In [41]: !cat sample2.csv
    guido, van, rossum
    spam, spam, spam
    
    In [42]: compare("sample1.csv", "sample2.csv")
    Different lines
    whatever, however, whenever
    guido, van, rossum
    bacon, eggs, mortar
    ---
    Common lines
    spam, spam, spam
    

