In Python, how can I get a line number corresponding to a given character location?
<p>If I have a text that I've read into memory by using <code>open('myfile.txt').read()</code>, and if I know a certain location in this file, say, at character 10524, how can I find the line number of that location? </p>

<p>Something along the lines of:</p>

<pre><code>txt = open('myfile.txt').read()
lineno = len([c for c in txt[:10524] if c == '\n'])
</code></pre>

python