我想将一个文本文件的行与另一个文本文件逐行匹配,我的嵌套for循环有问题,一定很简单,但我找不到,
for line1 in ratings:
cont1+=1
for line2 in movies:
cont2+=1
print(cont1,cont2)
我用这个循环简化了它,检查错误,外循环没有达到 cont=2,
1 1
1 2
1 3
1 4
1 5
1 6
1 7
.
.
.
1 157
>>>
您需要同时遍历两个文件
ratings= open('ratings.txt')
movies= open('movies.txt')
for rating, movie in itertools.izip(ratings, movies):
# do task
或者
ratings= open('ratings.txt').readlines()
movies= open('movies.txt').readlines()
for rating, movie in zip(ratings, movies):
# do task
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.
您的实现运行在 file1 中的每一行运行 file2 中的所有行,
实际的代码是,
for line1,line2 in zip(ratings,movies):
cont1+=1
cont2+=1
print(cont1,cont2)
with open('ratings.txt') as ratings, open('movies.txt') as movies:
for rating, movie in itertools.izip(ratings, movies):
# do task
This is an improvement on avasal's answer in that it picks up the lines iteratively (equivalent to the old xreadlines
that is now deprecated) rather than reading all the lines in via readlines()
. So it should read a line from each, then process them, then read another line etc, permitting Files Of Enormity where readlines
implementation would be limited to smaller files.
Note that izip
continues until one of the iterators stops, so you'll only get as many rows as the shorter file. I'm assuming that's correct, however, since the code implicitly assumes the two files match anyway.