比较两个csv文件中的内容

时间:2016-08-15 16:00:57

标签: python csv

所以我有两个csv文件。 Book1.csv的数据多于similarities.csv,因此我想提取Book1.csv similarities.csv中{strong} 出现的行 with open('Book1.csv', 'rb') as csvMasterForDiff: with open('similarities.csv', 'rb') as csvSlaveForDiff: masterReaderDiff = csv.reader(csvMasterForDiff) slaveReaderDiff = csv.reader(csvSlaveForDiff) testNotInCount = 0 testInCount = 0 for row in masterReaderDiff: if row not in slaveReaderDiff: testNotInCount = testNotInCount + 1 else : testInCount = testInCount + 1 print('Not in file: '+ str(testNotInCount)) print('Exists in file: '+ str(testInCount)) 到目前为止我有什么

Not in file: 2093
Exists in file: 0

然而,结果是

Book1.csv

我知道这是不正确的,因为similarities.csv中至少$(document).ready(function() { $(" a .follow").click(function() { var thiss = $(this); var state = thiss.attr("data-state"); var id = thiss.attr("id"); var follow = thiss.attr("data-follow"); alert(value + id + flag); $.post("<?php echo base_url()?>profile/insert_follow", { state: state, id: id, follow: follow }, function(data) { if (thiss.hasClass("follow")) { var abc = "<a class="btn btn-info btn-follow unfollow" data-state="2" data-follow="<? php echo $artist_info - > id ?>" data-id="<? php echo $this->session->userdata('username') ?>">Following</a>"; thiss.append(abc); } elseif(thiss.hasClass("unfollow")) { var abc1 = "<a class="btn btn-info btn-follow follow" data-state="2" data-follow=" <? php echo $artist_info->id ?> " data-id=" <? php echo $this->session->userdata('username') ?> ">Follow</a>"; thiss.append(abc1); } $('.followers').children('span').html(data); }); }); }); 中的前16个条目不存在于public function insert_follow(){ $id=$_POST['id']; $follows = $_POST['follows']; $state = $_POST['state']; $data1=array( 'id'=>$id, 'follows' =>$follows, 'state' =>$state ); $query=$this->hbmodel->follow_artist($data1); } 中。我做错了什么?

2 个答案:

答案 0 :(得分:1)

csv.reader对象是迭代器,这意味着您只能迭代一次。您应该使用列表/集进行包含检查,例如:

slave_rows = set(slaveReaderDiff)

for row in masterReaderDiff:
    if row not in slave_rows:
        testNotInCount += 1
    else:
        testInCount += 1

答案 1 :(得分:0)

将其转换为sets后,您可以执行大量set相关&amp;无需编写大量代码即可进行有用的操作。

slave_rows = set(slaveReaderDiff)
master_rows = set(masterReaderDiff)

master_minus_slave_rows = master_rows - slave_rows
common_rows = master_rows & slave_rows

print('Not in file: '+ str(len(master_minus_slave_rows)))
print('Exists in file: '+ str(len(common_rows)))

以下是您可以执行的各种set operations

相关问题