没有遍历csv文件python3的所有行 [英] Not Iterating through all lines from a csv file python3

查看:122
本文介绍了没有遍历csv文件python3的所有行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对python3还是很陌生,我敢肯定我的问题很基础. 我一直在网上寻找一些帮助,而我最接近的人来自线程 在PYTHON中的两个CSV文件中查找公共区域

I am quite new to python3 and I am sure my question is very basic. I have been looking online for some help and the closest I got came from thread Find Common Region in two CSV File in PYTHON

但是,在我看来,这似乎并没有遍历每一行并停在第一行. 因此,在我的第一个csv中,我有2行,可以这样说:

However in my case it seems it is not iterating through everyline and stop at the first one. SO in my first csv I have 2 lines, lets say:

A,1,A1

B,2,B2

现在在第二个csv中,我有一千行,类似于

now in my second csv I have a thousand lines, something like

A,1,B5

A,2,A2

B,2,C6

B,3,C7

C,3,D7

C,4,D8

......

我的代码如下:

read1 = csv.reader(csv1) 
for row1 in read1:
    read2 = csv.reader(csv2)
    for row2 in read2:
        if row1[0] == row2[0] and row1[1] == row2[1]:
           print('There is a match', row1[0], row1[1])

但是,我的输出是 有一场比赛A 1 它仅找到第一个匹配项,而不找到另一个匹配项:B 2 我不确定我的迭代中有什么问题:

However, my output is There is a match A 1 It only finds the first match and not the other matche: B 2 I am not sure what is wrong in my iterations:

预先感谢您的帮助

推荐答案

将内容放入列表中:

import  csv
with open(file1) as f1,open(file2) as f2:
    rd1, rd2 = csv.reader(f1) ,list(csv.reader(f2))
    for row1 in rd1:
        for row2 in rd2:
            if row1[0] == row2[0] and row1[1] == row2[1]:
                print('There is a match', row1[0], row1[1])

这篇关于没有遍历csv文件python3的所有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆