How to iterate across lines in two files simultaneously?

后端 未结 3 1579
闹比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:29

    Python 2:

    Use itertools.izip to join the two iterators.

    from itertools import izip
    for line_from_file_1, line_from_file_2 in izip(open(file_1), open(file_2)):
    

    If the files are of unequal length, use izip_longest.

    In Python 3, use zip and zip_longest instead. Also, use a with to open files, so that closing is handled automatically even in case of errors.

    with open(file1name) as file1, open(file2name) as file2:
        for line1, line2 in zip(file1, file2):
            #do stuff
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-30 10:35

    A complete example for posterity:

    from itertools import izip, izip_longest
    
    file1name = '/some/path/and/file'
    file2name = '/another/path/and/another/file'
    
    with open(file1name) as file1, open(file2name) as file2:
        for line1, line2 in izip(file1, file2):   # or izip_longest
            # do something with the lines
    

    Using with ensures the files are cleaned up after use, even if an exception occurs.

    0 讨论(0)
提交回复
热议问题