How to get the previous line in a file?

Qilong Ren qilong_ren at yahoo.com
Fri Mar 16 22:09:23 CET 2007


Hi, Shane,

Thanks for fast reply.

What I used is :
       for line in open(FILE):
           <do stuff>
I don't want to store all lines in a list because sometimes the file is very large. We need to store the value of the previous line in a variable. Is that right? 

Thanks,Qilong

----- Original Message ----
From: Shane Geiger <sgeiger at ncee.net>
To: Qilong Ren <qilong_ren at yahoo.com>
Sent: Friday, March 16, 2007 1:51:09 PM
Subject: Re: How to get the previous line in a file?

lines = open('/tmp/foo.py', 
"r").read().splitlines()                                                                                                    

previous_line = 
''                                                                                                                                      

for line in 
lines:                                                                                                                                      

    if "foo" in 
line:                                                                                                                                   

        print "found foo in the current line.  The previous line is:  " 
+ 
previous_line                                                                 

                                                                                                                                                        



Qilong Ren wrote:
> Hi,all
>
> I am new to this list. And I am glade I am here.
> I have a question. I need to do some text processing. I need to read 
> from a file line by line. If some line is met with some condition, the 
> previous line needs some modification. How to get the info of the 
> previous line?
>
> Thanks!
> Qilong
>
> ------------------------------------------------------------------------
> Never miss an email again!
> Yahoo! Toolbar 
> <http://us.rd.yahoo.com/evt=49938/*http://tools.search.yahoo.com/toolbar/features/mail/> 
> alerts you the instant new Mail arrives. Check it out. 
> <http://us.rd.yahoo.com/evt=49937/*http://tools.search.yahoo.com/toolbar/features/mail/>

-- 
Shane Geiger
IT Director
National Council on Economic Education
sgeiger at ncee.net  |  402-438-8958  |  http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy


begin:vcard
fn:Shane Geiger
n:Geiger;Shane
org:National Council on Economic Education (NCEE)
adr:Suite 215;;201 N. 8th Street;Lincoln;NE;68508;United States
email;internet:sgeiger at ncee.net
title:IT Director
tel;work:402-438-8958
x-mozilla-html:FALSE
url:http://www.ncee.net
version:2.1
end:vcard








 
____________________________________________________________________________________
Bored stiff? Loosen up... 
Download and play hundreds of games for free on Yahoo! Games.
http://games.yahoo.com/games/front
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070316/2865bd4a/attachment.html>


More information about the Python-list mailing list