简体   繁体   中英

Comparing columns on 2 csv files

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!

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM