Roy H. Han
starsareblueandfaraway at gmail.com
Fri Feb 27 12:03:38 EST 2009
I'm not sure what exactly you're trying to do, but you can keep track
of line numbers using 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:
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.
More information about the Python-list