Using xreadlines

2009-02-26 Thread Brett Hedges

Hi,

I am using both xreadlines and files iterators for a script that I need to 
finish. I am iterating over the entire file but stopping to use xreadlines to 
grab certain lines as strings to process them.

My question is how do I go to a previous line in the file? xreadlines has a 
file.next() statement that gives the next line, and I need a statement that 
gives me the previous line.

My script has a loop that looks for a certain word then breaks the loop. But I 
need to go to the previous line before I break since the script uses that line 
(which gets processed). I'm sure there is an easier way to do this but since I 
am new to python and have spent a lot of time on this script I can't really go 
back and change a lot of things unless there is no possible way. Something like 
the example below.

f.open("text.txt",'r')
files = f.xreadlines()
Name = "Section 1"


for line in f:
   if Name in line:
 while x<20
   field = files.next()
 if "2.6" in field:
  *files.previousline()*
   break
 x=x+1




Thanks,

Brett 


  
--
http://mail.python.org/mailman/listinfo/python-list


Re: Using xreadlines

2009-02-27 Thread Brett Hedges

> You can also keep track of the absolute position of the lines in the file, 
> etc, or step back looking for newlines, etc, but it's not handy.


How would I keep track of the absolute position of the lines? I have tried to 
use the files.seek() command with the files.tell() command and it does not seem 
to work. The files.tell() command seems to give me a number but when I use the 
files.next() command with xreadlines it does not change the line number the 
next time I use files.tell(). 

Thanks,

Brett

--- On Thu, 2/26/09, bearophileh...@lycos.com  wrote:

> From: bearophileh...@lycos.com 
> Subject: Re: Using xreadlines
> To: python-list@python.org
> Date: Thursday, February 26, 2009, 8:09 PM
> Brett Hedges:
> > My question is how do I go to a previous line in the
> file? xreadlines has a file.next() statement that gives the
> next line, and I need a statement that gives me the previous
> line.<
> 
> In modern versions of Python you usually don't need
> xreadlines,
> because files are iterable.
> 
> If your files are small, you can just read all the lines in
> a list
> with open(...).readlines(), and then just use the item of
> the list
> with the n-1 index.
> 
> If the file is quite large or you like to keep things lazy,
> then you
> have to keep memory of the previous line, using an
> auxiliary variable.
> You can also wrap this idiom into a generator function (or
> iterable
> class, probably) that yields items and keeps memory of the
> last one
> (but you can't ask the previous of the first item, of
> course).
> 
> You can also keep track of the absolute position of the
> lines in the
> file, etc, or step back looking for newlines, etc, but
> it's not handy.
> 
> Bye,
> bearophile
> --
> http://mail.python.org/mailman/listinfo/python-list


  
--
http://mail.python.org/mailman/listinfo/python-list