1

I have a CSV that looks something like this. The list will typically contain about 10-50 lines:

HASH                               FILE        USER     HOST
1C7E2110A55F42F525E196499FF88523   ABC.EXE     TEST1    XYZ
3B7E2110A55F42F525E196499AA88421   124.TXT     TEST2    SDF

I have a tuple previously defined in my script with two values in each set. The first containing a hash and the second containing another number. For example:

hashes = [('1C7E2110A55F42F525E196499FF88523', '4'), ('5A7E2110A55F42F525E196499FF88511', '89')]

I am trying to figure out how to compare my hashes tuple to my CSV, then add another column named "NUMBER" that populates with the second value in the tuple for any hash values that are both in my original CSV and my hashes tuple. I can figure out how to add another column to the CSV, but populating it with the proper data from my tuple is where I am hung up. So for the above examples, I want my final CSV output to look like this... granted, the CSV and tuple will be larger, but as an example:

HASH                               FILE        USER     HOST  NUMBER
1C7E2110A55F42F525E196499FF88523   ABC.EXE     TEST1    XYZ   4
3B7E2110A55F42F525E196499AA88421   124.TXT     TEST2    SDF
P.J.
  • 217
  • 2
  • 4
  • 13
  • 1
    There are several questions on adding a column t a file. In essense you need to write to a new file: http://stackoverflow.com/questions/11070527/how-to-add-a-new-column-to-a-csv-file-using-python, http://stackoverflow.com/questions/23682236/add-a-new-column-to-an-existing-csv-file, http://stackoverflow.com/questions/23261852/adding-column-in-csv-python-and-enumerating-it – doctorlove Jul 11 '16 at 13:25

1 Answers1

1

One approach is to first read hashes into dict, and then writing input csv into output by adding new column to each row using the dict.

Following is code sample based on your post description:

import csv

hashes = [('1C7E2110A55F42F525E196499FF88523', '4'), ('5A7E2110A55F42F525E196499FF88511', '89')]
hashdata = {}

for hashentry in hashes:
    hashdata[hashentry[0]] = hashentry

with open('input.csv','r') as csvinput:
    with open('output.csv', 'w') as csvoutput:
        writer = csv.writer(csvoutput, lineterminator='\n')
        reader = csv.reader(csvinput)


        all = []
        titlerow = next(reader)
        titlerow.append('NUMBER')
        all.append(titlerow)

        for row in reader:
            if row[0] in hashdata:
                row.append(hashdata[row[0]][1])
                all.append(row)
            else:
                all.append(row)

        writer.writerows(all)
Kevin
  • 901
  • 1
  • 7
  • 15