擅长:python、mysql、java
<p>regex可能是最简单的解决方案。在</p>
<pre><code>>>> textdata = '''
someline
another line
real HOSTNAME
address 127.0.0.1
post 1
post 2
'''
>>> re.findall('^(.*)$\n^.*$\naddress 127.0.0.1', textdata, re.MULTILINE)
['real HOSTNAME']
</code></pre>
<p>您也可以使用<a href="http://docs.python.org/library/linecache.html#module-linecache" rel="nofollow">linecache module</a>,或者简单地使用<code>f.readlines()</code>将所有行读入一个列表中。在</p>