<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[How to iterate over every n-th line from a file?]]></title><description><![CDATA[<p dir="auto">I have a file called data.txt containing lines of data:</p>
<pre><code>This is line one
This is line two 
This is line three 
This is line four 
This is line five 
This is line six 
This is line seven
</code></pre>
<p dir="auto">…<br />
I have the following code:</p>
<pre><code>with open('data.txt', 'r') as f:
    for x, line in enumerate(f):
        if x == 3:
            print(line)
In this case it only prints
</code></pre>
<p dir="auto">“This is line four”.<br />
I do understand why but how do I take it from here and have it print the lines 4, 7, 10, 13, …?</p>
]]></description><link>https://community.secnto.com//topic/2097/how-to-iterate-over-every-n-th-line-from-a-file</link><generator>RSS for Node</generator><lastBuildDate>Mon, 08 Jun 2026 22:41:58 GMT</lastBuildDate><atom:link href="https://community.secnto.com//topic/2097.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 02 Dec 2020 12:55:00 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How to iterate over every n-th line from a file? on Wed, 02 Dec 2020 12:57:01 GMT]]></title><description><![CDATA[<p dir="auto">Demo:</p>
<p dir="auto"><strong>data.txt:</strong></p>
<pre><code>line1
line2
line3
line4
line5
line6
line7
line8
line9
line10
line11
line12
line13
</code></pre>
<p dir="auto">Code:</p>
<pre><code>from itertools import islice

with open('data.txt') as f:
    for line in islice(f, 3, None, 3):
        print line,  # Python3: print(line, end='')
</code></pre>
<p dir="auto">Produces:</p>
<pre><code>line4
line7
line10
line13
</code></pre>
<p dir="auto"><a href="https://stackoverflow.com/questions/36487709/how-to-iterate-over-every-n-th-line-from-a-file" target="_blank" rel="noopener noreferrer nofollow ugc">Reff</a></p>
]]></description><link>https://community.secnto.com//post/6172</link><guid isPermaLink="true">https://community.secnto.com//post/6172</guid><dc:creator><![CDATA[zaasmi]]></dc:creator><pubDate>Wed, 02 Dec 2020 12:57:01 GMT</pubDate></item></channel></rss>