[Tutor] Match on current line and next line. Possible?

Kent Johnson kent37 at tds.net
Tue Feb 8 17:16:36 CET 2005


Hmm, this would be a good use of itertools.tee() (Python 2.4 only):

import itertools
iter1, iter2 = itertools.tee(open('/somefile', 'r'))
iter2.next()
for line, next_line in izip(iter1, iter2):
   ...

Kent

Pierre Barbier de Reuille wrote:
> MMmh ... one way to do that :
> 
> Py> file_content = open('/somefile', 'r').readlines()
> Py> next_file_content = iter(file_content)
> Py> next_file_content.next()
> Py> for (line, next_line) in izip(file_content, next_file_content):
> Py>         match_one = re.search('^Python', line)
> Py>         match_two = re.search('^\tBLAH', line)
> Py>         if match_one and nextline == match_two:
> Py>                do_something()
> 
> Pierre
> 
> Tom Tucker a écrit :
> 
>> Hello! How can I instruct Python to match on the current line and the
>> next line?
>>
>>
>> Assumptions;
>> - We are reading in one line at a time
>>
>>
>> BROKEN EXAMPLE (discussion)
>> ######################
>> file = open('/somefile','r').readlines()
>> for line in file:
>>         match_one = re.search('^Python', line)
>>         match_two = re.search('^\tBLAH', line)
>>         if match_one and nextline == match_two:                
>> do_something()
>>
>>
>> Maybe this just isn't possible, since we are working line by line.
>> Any suggestions?
>>
>> Tom
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> http://mail.python.org/mailman/listinfo/tutor
>>
> 



More information about the Tutor mailing list