[英]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文件A中提取2列(电子邮件地址和电话号码),并将其与csv文件B中的2列(也包括电子邮件地址和电话号码)进行比较。
csv file A columns in order: Email address, Phone number csv文件A列的顺序为:电子邮件地址,电话号码
csv file B columns in order: Email address, Address, Department, Location, Phone number, Hire Date csv文件B列的顺序为:电子邮件地址,地址,部门,位置,电话号码,雇用日期
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. 我想要的是从csv文件A中提取2列,并将其与csv文件B中的2个指定列进行比较。
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 在csv文件B中,如果电子邮件地址没有关联的电话号码,它将与csv文件A进行比较,然后将电话号码复制到文件B
I was testing with the code, (im new to programming), but am unsure how grab 2 columns. 我正在测试代码(对编程不熟悉),但是不确定如何抓取2列。 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.
我曾考虑过将两个文件中的用户名和密码放入一个Dict中,并比较两个Dict,但是我不确定如何从列中获取数据。
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? 我将看一下以下内容: 从csv文件创建字典?
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.