[Tutor] Simple text file processing using fileinput module."Grabbing successive lines" failure

Flynn, Stephen (L & P - IT) Steve.Flynn at capita.co.uk
Tue Jul 3 11:54:22 CEST 2012


Hi Peter,

> As you already learned the csv module is the best tool to address your
> problem.

Yup - it's almost going to make my little coding exercise trivial. :)

> However, I'd like to show a generic way to get an extra item in a for-
> loop.

[snip]

> Here's a self-contained example:
> 
> >>> items = "alpha- beta gamma- delta- epsilon zeta".split()
> >>> it = iter(items)
> >>> for item in it:
> ...     while item.endswith("-"):
> ...             item += next(it)
> ...     print item
> ...
> alpha-beta
> gamma-delta-epsilon
> zeta

Perfect - that's essentially what I was trying to do, but I doubt it
would have occurred to me to create my own iterator and make use of it.

Thanks for this example too - nice and easy to understand.

Steve.


This email and any attachment to it are confidential.  Unless you are the intended recipient, you may not use, copy or disclose either the message or any information contained in the message. If you are not the intended recipient, you should delete this email and notify the sender immediately.

Any views or opinions expressed in this email are those of the sender only, unless otherwise stated.  All copyright in any Capita material in this email is reserved.

All emails, incoming and outgoing, may be recorded by Capita and monitored for legitimate business purposes. 

Capita exclude all liability for any loss or damage arising or resulting from the receipt, use or transmission of this email to the fullest extent permitted by law.


More information about the Tutor mailing list