How can I store in a variable certain lines in python?
<p>I have this code: </p>

<pre><code>with open("/etc/network/interfaces", "r") as file:
    content = file.read()
    print content
</code></pre>

<p>it's working and showing this:</p>

<p><a href="http://i.stack.imgur.com/Zauen.png" rel="nofollow"><img src="http://i.stack.imgur.com/Zauen.png" alt="enter image description here"></a></p>

<p>How can I store in a variable any word and print that word?</p>

<blockquote>
  <p>For example, I want to store the word (lo) in the second line in a variable and then print that variable.</p>
</blockquote>

<p>Use the re module in Python's builtin library:</p>

<pre><code>import re

with open("/etc/network/interfaces", "r") as file:
    content = file.readlines()

target_word = re.findall("lo()", content[3]) # save the string 'lo' into target word
print ''.join(target_word) # prints: lo
</code></pre>

<p>If you plan on trying to find more words, just add more words to the regular expression. For example, if you wanted to find the words lo and auto, you would say <code>re.findall("lo|auto", content)</code>. If you use this method, I recommend reading the <a href="https://docs.python.org/3/library/re.html" rel="nofollow">official documentation on the <code>re</code> module</a>.</p>

python linux python-2.7 python-3.x debian