Incorrect line number returned on specifying Start parameter in enumerate Python
<p>I have slight confusion regarding the start parameter in enumerate function,as i recently started working on python i don't have much idea how it is supposed to work.
Suppose i have an example file below:</p>

<pre><code>Test 1
Test 2
Test 3
</code></pre>

<p>This is the first line <code>[SB WOM]|[INTERNAL REQUEST]|[START]</code> which is the start of message</p>

<pre><code>Name : Vaibhav
Designation : Technical Lead
ID : 123456
Company : Nokia
</code></pre>

<p>This is the sixth line <code>[SB WOM]|[INTERNAL REQUEST]|[END]</code> which is the end of message</p>

<p>Now when i run the below code :</p>

<pre><code>path =("C:/Users/vgupt021/Desktop")
in_file = os.path.join(path,"KSClogs_Test.txt")
fd = open(in_file,'r')
for linenum,line in enumerate(fd) :
    if "[SB WOM]|[INTERNAL REQUEST]|[START]" in line:
        x1 = linenum
        print x1
        break 
for linenum,line in enumerate(fd,x1):
    if "[SB WOM]|[INTERNAL REQUEST]|[END]" in line:
        print linenum
        break
</code></pre>

<p>I get the linenum returned as 3 and 7, I am not clear why it is not returned as 3,8.Since the index number of line <code>"[SB WOM]|[INTERNAL REQUEST]|[END]"</code> is 8 and not 7, how the start parameter makes the difference in second part of the loop.</p>

<p>Since the file iterator object has read the first four lines, when running the second <code>for</code> loop, it starts from where it stopped. The previous iteration stopped at line 3 (assuming we start counting from 0), the next for loop starts at line 4. </p>

<p>Therefore, the <code>enumerate</code> of the second loop should start from <code>x1 + 1</code> not <code>x1</code> as the line with index <code>x1</code> was already covered in the previous loop; last line of first loop:</p>

<pre><code>for linenum, line in enumerate(fd, x1+1):
    ...
</code></pre>

python