Re: iterating over a file with two pointers

2013-09-19 Thread Oscar Benjamin
On 19 September 2013 15:38, Peter Otten <__pete...@web.de> wrote: >> While running the above python.exe was using 6MB of memory (according >> to Task Manager). I believe this is because tee() works as follows >> (which I made up but it's how I imagine it). > > [...] > >> However, when I ran the abo

Re: iterating over a file with two pointers

2013-09-19 Thread Peter Otten
Oscar Benjamin wrote: > $ cat tee.py > #!/usr/bin/env python > > import sys > from itertools import tee > > items = iter(range(int(sys.argv[1]))) > > while True: > for x in items: > items, discard = tee(items) > break > else: > break > > print(x) > > $ time py

Re: iterating over a file with two pointers

2013-09-19 Thread Oscar Benjamin
On 19 September 2013 08:23, Peter Otten <__pete...@web.de> wrote: > Roy Smith wrote: >> >> I believe by "Peter's version", you're talking about: >> >>> from itertools import islice, tee >>> >>> with open("tmp.txt") as f: >>> while True: >>> for outer in f: >>> print outer, >

Re: iterating over a file with two pointers

2013-09-19 Thread Peter Otten
Roy Smith wrote: >> Dave Angel wrote (and I agreed with): >>> I'd suggest you open the file twice, and get two file objects. Then you >>> can iterate over them independently. > > > On Sep 18, 2013, at 9:09 AM, Oscar Benjamin wrote: >> There's no need to use OS resources by opening the file twi

Re: iterating over a file with two pointers

2013-09-19 Thread Joshua Landau
Although "tee" is most certainly preferable because IO is far slower than the small amounts of memory "tee" will use, you do have this option: def iterate_file_lines(file): """ Iterate over lines in a file, unlike normal iteration this allows seeking. """

Re: iterating over a file with two pointers

2013-09-18 Thread Steven D'Aprano
On Wed, 18 Sep 2013 04:12:05 -0700, nikhil Pandey wrote: > hi, > I want to iterate over the lines of a file and when i find certain > lines, i need another loop starting from the next of that "CERTAIN" line > till a few (say 20) lines later. so, basically i need two pointers to > lines (one for ou

Re: iterating over a file with two pointers

2013-09-18 Thread Steven D'Aprano
On Wed, 18 Sep 2013 05:14:23 -0700, nikhil Pandey wrote: > I want to iterate in the inner loop by reading each line till some > condition is met.how can i do that. Thanks for this code. while not condition: read line Re-write using Python syntax, and you are done. -- Steven -- https://

Re: iterating over a file with two pointers

2013-09-18 Thread Dave Angel
On 18/9/2013 10:36, Roy Smith wrote: >> Dave Angel wrote (and I agreed with): >>> I'd suggest you open the file twice, and get two file objects. Then you >>> can iterate over them independently. > > > On Sep 18, 2013, at 9:09 AM, Oscar Benjamin wrote: >> There's no need to use OS resources by op

Re: iterating over a file with two pointers

2013-09-18 Thread Travis Griggs
On Sep 18, 2013, at 5:07 AM, nikhil Pandey wrote: > On Wednesday, September 18, 2013 4:51:51 PM UTC+5:30, Chris Angelico wrote: >> On Wed, Sep 18, 2013 at 9:12 PM, nikhil Pandey >> wrote: >> >>> hi, >> >>> I want to iterate over the lines of a file and when i find certain lines, i >>> need

Re: iterating over a file with two pointers

2013-09-18 Thread Roy Smith
> Dave Angel wrote (and I agreed with): >> I'd suggest you open the file twice, and get two file objects. Then you >> can iterate over them independently. On Sep 18, 2013, at 9:09 AM, Oscar Benjamin wrote: > There's no need to use OS resources by opening the file twice or to > screw up the IO c

Re: iterating over a file with two pointers

2013-09-18 Thread Oscar Benjamin
On 18 September 2013 13:56, Roy Smith wrote: > >> > On Wed, Sep 18, 2013 at 9:12 PM, nikhil Pandey >> > wrote: >> >> hi, >> >> I want to iterate over the lines of a file and when i find certain lines, >> >> i need another loop starting from the next of that "CERTAIN" line till a >> >> few (say 20

Re: iterating over a file with two pointers

2013-09-18 Thread Roy Smith
> > On Wed, Sep 18, 2013 at 9:12 PM, nikhil Pandey > > wrote: > >> hi, > >> I want to iterate over the lines of a file and when i find certain lines, > >> i need another loop starting from the next of that "CERTAIN" line till a > >> few (say 20) lines later. > >> so, basically i need two point

Re: iterating over a file with two pointers

2013-09-18 Thread Peter Otten
nikhil Pandey wrote: > On Wednesday, September 18, 2013 5:14:10 PM UTC+5:30, Peter Otten wrote: > I want to iterate in the inner loop by reading each line till some > condition is met.how can i do that. Thanks for this code. That's not what I had in mind when I asked you to >> describe your act

Re: iterating over a file with two pointers

2013-09-18 Thread nikhil Pandey
On Wednesday, September 18, 2013 5:14:10 PM UTC+5:30, Peter Otten wrote: > nikhil Pandey wrote: > > > > > hi, > > > I want to iterate over the lines of a file and when i find certain lines, > > > i need another loop starting from the next of that "CERTAIN" line till a > > > few (say 20) lines

Re: iterating over a file with two pointers

2013-09-18 Thread nikhil Pandey
On Wednesday, September 18, 2013 4:51:51 PM UTC+5:30, Chris Angelico wrote: > On Wed, Sep 18, 2013 at 9:12 PM, nikhil Pandey > wrote: > > > hi, > > > I want to iterate over the lines of a file and when i find certain lines, i > > need another loop starting from the next of that "CERTAIN" line

Re: iterating over a file with two pointers

2013-09-18 Thread Peter Otten
nikhil Pandey wrote: > hi, > I want to iterate over the lines of a file and when i find certain lines, > i need another loop starting from the next of that "CERTAIN" line till a > few (say 20) lines later. so, basically i need two pointers to lines (one > for outer loop(for each line in file)) and

Re: iterating over a file with two pointers

2013-09-18 Thread Dave Angel
On 18/9/2013 07:21, Chris Angelico wrote: > On Wed, Sep 18, 2013 at 9:12 PM, nikhil Pandey > wrote: >> hi, >> I want to iterate over the lines of a file and when i find certain lines, i >> need another loop starting from the next of that "CERTAIN" line till a few >> (say 20) lines later. >> so

Re: iterating over a file with two pointers

2013-09-18 Thread Chris Angelico
On Wed, Sep 18, 2013 at 9:12 PM, nikhil Pandey wrote: > hi, > I want to iterate over the lines of a file and when i find certain lines, i > need another loop starting from the next of that "CERTAIN" line till a few > (say 20) lines later. > so, basically i need two pointers to lines (one for out