Note that there are some explanatory texts on larger screens.

plurals
  1. POPython nested loop - get next N lines
    primarykey
    data
    text
    <p>I'm new to Python and trying to do a nested loop. I have a very large file (1.1 million rows), and I'd like to use it to create a file that has each line along with the next N lines, for example with the next 3 lines:</p> <pre><code>1 2 1 3 1 4 2 3 2 4 2 5 </code></pre> <p>Right now I'm just trying to get the loops working with rownumbers instead of the strings since it's easier to visualize. I came up with this code, but it's not behaving how I want it to:</p> <pre><code>with open('C:/working_file.txt', mode='r', encoding = 'utf8') as f: for i, line in enumerate(f): line_a = i lower_bound = i + 1 upper_bound = i + 4 with open('C:/working_file.txt', mode='r', encoding = 'utf8') as g: for j, line in enumerate(g): while j &gt;= lower_bound and j &lt;= upper_bound: line_b = j j = j+1 print(line_a, line_b) </code></pre> <p>Instead of the output I want like above, it's giving me this: </p> <pre><code>990 991 990 992 990 993 990 994 990 992 990 993 990 994 990 993 990 994 990 994 </code></pre> <p>As you can see the inner loop is iterating multiple times for each line in the outer loop. It seems like there should only be one iteration per line in the outer loop. What am I missing?</p> <p>EDIT: My question was answered below, here is the exact code I ended up using: </p> <pre><code>from collections import deque from itertools import cycle log = open('C:/example.txt', mode='w', encoding = 'utf8') try: xrange except NameError: # python3 xrange = range def pack(d): tup = tuple(d) return zip(cycle(tup[0:1]), tup[1:]) def window(seq, n=2): it = iter(seq) d = deque((next(it, None) for _ in range(n)), maxlen=n) yield pack(d) for e in it: d.append(e) yield pack(d) for l in window(open('c:/working_file.txt', mode='r', encoding='utf8'),100): for a, b in l: print(a.strip() + '\t' + b.strip(), file=log) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload