How to skip to a line via character seeking in Python
<p>If I have a text file that has a bunch of random text before I get to the stuff I actually want, how do I move the file pointer there? </p>

<p>Say for example my text file looks like this:</p>

<pre><code>#foeijfoijeoijoijfoiej ijfoiejoi jfeoijfoifj  i jfoei joi jo ijf eoij oie jojf
#feoijfoiejf   ioj oij       oi jo ij   i joi jo ij oij  ####  oijroijf 3## # o
#foeijfoiej i jo i  iojf 3 ##  #io joi joij oi j## io joi joi j3# 3i ojoi joij
# The stuff I care about
</code></pre>

<p>(The hashtags are a part of the actual text file)</p>

<p>How do I move the file pointer to the line of stuff I care about, and then how would I get python to tell me the number of the line, and start the reading of the file there?</p>

<p>I've tried doing a loop to find the line that the last hashtag is in, and then reading from there, but I still need to get rid of the hashtag, and need the line number.</p>

<p>Try using the <a href="https://docs.python.org/3/library/io.html#io.IOBase.readlines" rel="nofollow">readlines</a> function. This will return a list containing each line. You can use a <code>for</code> loop to parse through each line, searching for what you need, then obtain the number of the line via its index in the list. For instance:</p>

<pre><code>with open('some_file_path.txt') as f:
    contents = f.readlines()
object = '#the line I am looking for'
for line in contents:
    if object in line:
        line_num = contents.index(object)
</code></pre>

<p>To get rid of the pound sign, just use the <a href="https://docs.python.org/3/library/stdtypes.html?highlight=replace#str.replace" rel="nofollow">replace</a> function. Eg. <code>new_line = line.replace('#','')</code></p>

python loops text