Python从两个文件的嵌套循环中读取行 [英] Python reading lines in nested for loops from two files

查看:422
本文介绍了Python从两个文件的嵌套循环中读取行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想逐行将一个文本文件的行与另一个文本文件进行匹配,嵌套的for循环有问题,它一定很简单,但我找不到它,

I want to match the lines of one text file with another text file, line by line, there is a problem with my nested for loops, it must be simple, but I can't find it,

for line1 in ratings:
    cont1+=1

    for line2 in movies:
         cont2+=1
         print(cont1,cont2)

我通过此循环对其进行了简化,以检查错误, 外部循环未达到cont = 2

I simplified it with this loops, to check the error, The outer loop doesn't reach cont=2,

1 1
1 2
1 3
1 4
1 5
1 6
1 7
.
.
.
1 157
>>>

推荐答案

问题是,由于每个人的答复,我需要使用seek(0)重置内部循环,以确保循环正常进行.

The problem is that I need to reset the inner loop with seek(0), doing that the loop works properly, thanks to everyone for your replies.

这篇关于Python从两个文件的嵌套循环中读取行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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