简体   繁体   中英

Using two loops of itertools

I am trying to run two loops of itertools but I am getting an error. This is my code:

import itertools
with open('lognew.txt', 'r') as f:
    firstlines = itertools.islice(f, 0, None, 1)
    secondlines = itertools.islice(f, 0, None, 2)

    for (line1 in firstlines) and (line2 in secondlines):
        print line1 + line2 

I am getting this error :

File "C:\Users\abhi\Desktop\logedit.py", line 15
    for (line1 in firstlines) and (line2 in secondlines):
                            ^
SyntaxError: invalid syntax

I want the result as this:

<first line in firstlines> is meaning of  <first line in secondlines>
<second line in firstlines>  is meaning of <second line in secondlines>
<third line in firstlines>  is meaning of <third line in secondlines>

There is no error in reading files as the following code prints lines successfully.

for line1 in firstlines :
    print line1
for line2 in secondlines :
    print line2

Any help is highly appreciated.

Your slices won't do what you think they'll do. The first slice gives you just the first line, the second slice gives you the second next line , provided you iterate over it after the first slice. That's because the input file has already advanced one line, so the second slice object will skip one, give you the next, on iteration. You'd line 1, then line 3, then line 4, then line 6, etc. if you had zipped the files.

What you really want, is pair-wise line looping. For that, you don't need to use slicing at all; use itertools.izip() ; it'll give you every two lines zipped together:

from itertools import izip

with open('lognew.txt', 'r') as f:
    for line1, line2 in izip(f, f):
        print line1.rstrip('\n') + line2

Every iteration, izip() will get a value from the first argument (open file object f ), then from the second argument ( also open file object f ). Because a file object advances to the next line every time it is iterated over, the above effectively gives you the lines from the file in pairs.

Note that if the file has an odd number of lines, you'll not see the last line. If you need access to that last line, use itertools.izip_longest() instead:

from itertools import izip_longest

with open('lognew.txt', 'r') as f:
    for line1, line2 in izip_longest(f, f, fillvalue='\n'):
        print line1.rstrip('\n') + line2

which will add an empty line ( fillvalue='\\n' ) if the number of lines is odd.

In Python 3, itertools.izip() has replaced the built-in zip() function, and itertools.izip_longest() has been renamed to itertools.zip_longest() .

for line1, line2 in itertools.izip(f1, f2):

Python双迭代

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM