 <p>To skip lines that do not match those strings add a check:</p> <pre><code>if any(bool(x) for x in d, HD, HA, M): print ... output.write </code></pre> <p>Try running the script in a debugger:</p> <pre><code>$ python -m pdb your_script.py </code></pre> <p>and see what variables are there and what's wrong. Since PDB is not convenient, you might want to install <a href="http://pypi.python.org/pypi/ipdb" rel="nofollow"><code>ipdb</code></a> or <a href="http://pypi.python.org/pypi/pudb" rel="nofollow"><code>pudb</code></a>.</p>