我正在尝试比较2个CSV文件,其中第一个只有几个条目,而另一个只有300行。我尝试达到的输出是如果file1.csv的列电子邮件与file2.csv的列电子邮件匹配但日期不匹配,则从大文件中打印行的输出。我想使用“ CSV”模块执行此操作。这是一个示例:
file1.csv(条目很少):
Tom,Johns,tom.johns@email.com,14 May 2017
Imaginary,User,imaginary@email.com,23 May 2018
file2.csv(大文件):
First,User,first.user@email.com,10 Jan 2018
Second,User,second.user@email.com,13 Feb 2018
Tom,Johns,tom.johns@email.com,16 Jun 2017
Imaginary,User,imaginary@email.com,23 May 2018
result.csv(所需结果)
Tom,Johns,tom.johns@email.com,16 Jun 2017
我尝试使用功能“ next”实现此目的,但是由于file1.csv中只有2个条目,脚本停止了。我已经重写了代码,但是现在我正在关闭文件上进行I / O操作。
import csv
with open('file1.csv', 'r') as first_csv:
dialect = csv.excel()
file1 = csv.reader(first_csv, dialect)
with open('file2.csv', 'r') as second_csv:
dialect = csv.excel()
file2 = csv.reader(second_csv, dialect)
writer = csv.writer(open('result.csv', 'w'))
output = set()
for row1 in file1:
for row2 in file2:
if (row1[2] == row2[2]) and (row1[3] != row1[3]):
writer.writerow(row2)
output.append(row2)
答案 0 :(得分:0)
正如@ Mr.Zeus在评论中已经说过的那样,只要您正在读取或写入csv文件,它们就必须保留open
。因此,您不能在此处使用with
语句,该语句会在退出时关闭文件。
您的代码的另一个问题是(row1[3] != row1[3])
部分,通常是False
:D
所以这是固定版本。请注意close
块中的finally
,即使在try
部分中发生异常,也可以确保始终发生这种情况。
import csv
first_csv = open('file1.csv', 'r')
second_csv = open('file2.csv', 'r')
output_csv = open('result.csv', 'w')
try:
file1 = csv.reader(first_csv, csv.excel())
file2 = csv.reader(second_csv, csv.excel())
writer = csv.writer(output_csv)
output = []
for row1 in file1:
for row2 in file2:
if (row1[2] == row2[2]) and (row1[3] != row2[3]):
writer.writerow(row2)
output.append(row2)
finally:
first_csv.close()
second_csv.close()
output_csv.close()
print(output)
答案 1 :(得分:0)
我设法通过将CSV转换为列表来解决此问题。这是最终代码:
import csv
first_csv = open('file1.csv', 'r')
second_csv = open('file2.csv', 'r')
output_csv = open('result.csv', 'w')
file1 = csv.reader(first_csv, csv.excel())
file2 = csv.reader(second_csv, csv.excel())
writer = csv.writer(output_csv)
output = []
list1 = list(file1)
list2 = list(file2)
for row1 in list1:
for row2 in list2:
if (row1[2] == row2[2]) and (row1[3] != row2[3]):
writer.writerow(row2)
output.append(row2)
first_csv.close()
second_csv.close()
output_csv.close()
print(output)
感谢大家的帮助。