Reputation: 381
I am writing a small script that takes 2 columns (email address and phone number) from csv file A, and comparing it to 2 columns (also email address and phone number) from csv file B.
csv file A columns in order: Email address, Phone number
csv file B columns in order: Email address, Address, Department, Location, Phone number, Hire Date
What I would like is take the 2 columns from csv file A, and compare it to the 2 specified columns in csv file B.
In csv file B, If an email address does not have a phone number associated, it will compare it to csv file A, and copy the phone number over to file B
I was testing with the code, (im new to programming), but am unsure how grab 2 columns. I have thought of putting the username and password from both files into a Dict, and comparing the two Dict, but I am not sure how to grab the data from the columns.
import csv
def compareCSVCol():
cybReader = csv.reader(open(r"C:/JostleMobileNumberCSV/CYBMobile.csv"))
josReader = csv.reader(open(r"C:/JostleMobileNumberCSV/jostleContributors.csv"))
for i in cybReader:
print(i[0])
Thank you for your help!
Upvotes: 0
Views: 241
Reputation: 548
I would take a look at the following: Creating a dictionary from a csv file?
phoneDict = dict((row[0],row[1]) for row in cybReader)
with open('./out.csv', 'w') as outFile:
writer = csv.writer(outFile)
for row in josReader:
if not row[4]:
row[4] = phoneDict[row[0]]
writer.writerow(row)
Upvotes: 1