3

I am trying to compare values of a particular column between 2 csv. I tried the following code for the same. However, I am not getting any output and no error too. Please help me with this

with open("File1.csv", "rb") as in_file1, open("File2.csv", "rb") as in_file2,open("File3.csv", "wb") as out_file:
   reader1 = csv.reader(in_file1)
   reader2 = csv.reader(in_file2)
   writer = csv.writer(out_file)
   for row2 in reader2:
       for row1 in reader1:
           if row2[0] == row1[0]:
               row2[1] = row1[1]
       writer.writerow(row2)

Here is how the data looks like:

File 1

A 100
B 200
C 300
D 400
E 500

FIle 2

A
C
E
E
E
D

File 3 (Should be)

A 100
C 300
E 500
E 500
E 500
D 400
Lev Levitsky
  • 63,701
  • 20
  • 147
  • 175

2 Answers2

2

File1.csv is a mapping. Read it first and store it in a dictionary. Then iterate over File2.csv and write it to File3.csv together with the value retrieved from the mapping dictionary.

The following code works for your example:

with open("File1.csv", "rb") as in_file1:
    d = dict(csv.reader(in_file1, delimiter=' '))

with open("File2.csv", "rb") as in_file2, open("File3.csv", "wb") as out_file:
    writer = csv.writer(out_file, delimiter=' ')
    for rec in csv.reader(in_file2, delimiter=' '):
        writer.writerow((rec[0], d[rec[0]]))

Just for an illustration, d looks like this:

{'A': '100', 'B': '200', 'C': '300', 'D': '400', 'E': '500'}

The values are strings (not integers), but this is not a problem, since we are just printing them into a file.

eumiro
  • 207,213
  • 34
  • 299
  • 261
1

Why not simply use it this way:

lookup = {}                                                                     
with open('file1', 'r') as f:                                                   
    lookup = dict([l.split() for l in f.read().split('\n') if len(l) > 0])         
with open('file2', 'r') as file2, open('out', 'w') as out:                      
    for line in file2.readlines():                                              
        line = line.strip()                                                     
        out.write("%s %s\n" % (line, lookup[line]))

I don't see a point using csv here

Vinzenz
  • 2,749
  • 17
  • 23