 <p>Instead of :</p> <pre><code>if len(line) &lt;= 1: # only '\n' in «empty» lines break values = line.split() </code></pre> <p>try this:</p> <pre><code>values = line.split() if not values: # line is wholly whitespace, end of segment break </code></pre>