How to iterate across lines in two files simultaneously?

后端 未结 3 1596
闹比i
闹比i 2020-11-30 09:58

I have two files, and I want to perform some line-wise operation across both of them. (In other words, the first lines of each file correspond, as do the second, etc.) Now,

3条回答
  •  北荒
    北荒 (楼主)
    2020-11-30 10:31

    You could try

    for line1, line2 in zip(file1, file2):
        #do stuff
    

    Careful though, this loop will exit when the shorter file ends.

    When using Python 2, itertools.izip is better for this sort of thing because it doesn't create a list.

提交回复
热议问题