Using xreadlines
Roy H. Han
starsareblueandfaraway at gmail.com
Fri Feb 27 12:03:38 EST 2009
Brett,
I'm not sure what exactly you're trying to do, but you can keep track
of line numbers using itertools.
import itertools
for lineIndex, line in itertools.izip(itertools.count(1), open('text.txt')):
print lineIndex, line
Here is sample code for breaking on a word and returning the previous line
def breakOnWord(filePath, word):
previousLine = ''
for line in open(filePath):
if word in line:
return previousLine
previousLine = line
On Fri, Feb 27, 2009 at 11:43 AM, <bearophileHUGS at lycos.com> wrote:
> Brett Hedges:
>> How would I keep track of the absolute position of the lines?
>
> You may have to do all things manually (tell, seek and looking for
> newlines manually, iterating chars), that's why I have said it's not
> handy. The other solutions are simpler.
>
> Bye,
> bearophile
> --
> http://mail.python.org/mailman/listinfo/python-list
>
More information about the Python-list
mailing list