Python print next line while printing from for loop
<p>I am performing a for loop on a list and would like to find a match and print the next line.  However, when I try to use the next() method I keep getting a failure. Can I please get some help in extracting the next line after a specified match?</p>

<p>string (for loop output):</p>

<pre><code>item_0
0
item_1
0           




item_3
727

item_4
325
</code></pre>

<p>For Loop to find match and next line:</p>

<pre><code>result = tree.xpath('//tr/td/font/text()')

for line in result:
    if 'item_3' in line:
        print(line.next())
</code></pre>

<p>Error:</p>

<pre><code>AttributeError: '_ElementStringResult' object has no attribute 'next'
</code></pre>

<p><code>line</code> is a <code>lxml.etree._ElementStringResult</code> (a modified <code>str</code>) in your code. <code>lxml.etree._ElementStringResults</code> do not have a <code>next</code> method which is why you are getting an <code>AttributeError</code>. </p>

<p>You can set a flag indicating that the next line should be printed as follows:</p>

<pre><code>print_line = False
for line in result:
    if print_line:
        print(line)
        print_line = False
    if 'item_3' in line:
        print_line = True
</code></pre>

python match