newbie question: if var1 == var2:
John Machin
sjmachin at lexicon.net
Thu Dec 11 20:59:38 EST 2008
More information about the Python-list mailing list
Thu Dec 11 20:59:38 EST 2008
- Previous message (by thread): newbie question: if var1 == var2:
- Next message (by thread): newbie question: if var1 == var2:
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Dec 12, 11:39 am, MRAB <goo... at mrabarnett.plus.com> wrote: > Jason Scheirer wrote: > > On Dec 11, 3:49 pm, John Machin <sjmac... at lexicon.net> wrote: > >> On Dec 12, 10:31 am, "Rhodri James" <rho... at wildebst.demon.co.uk> > >> wrote: > > >>> On Thu, 11 Dec 2008 19:49:23 -0000, Steve Holden <st... at holdenweb.com> > >>> wrote: > >>>> Kirk Strauser wrote: > >>>>> At 2008-11-29T04:02:11Z, Mel <mwil... at the-wire.com> writes: > >>>>>> You could try > >>>>>> for item in fname: > >>>>>> item = item.strip() > >>>>> This is one case where I really miss Perl's "chomp" function. It > >>>>> removes a > >>>>> trailing newline and nothing else, so you don't have to worry about > >>>>> losing > >>>>> leading or trailing spaces if those are important to you. > >>>> ... and it's so hard to write > >>>> item = item[:-1] > >>> Tsk. That would be "chop". "chomp" would be > >>> if item[-1] == '\n': > >>> item = item[:-1] > >> Better: > >> if item and item[-1] == '\n': > >> return item[:-1] > >> return item > > > Best: > > > return item \ > > if not (item and item.endswith('\n')) \ > > else item[:-1] > > > Though really you should be using item.rstrip() > > Why not just: > > item[:-1] if item.endswith('\n') else item Some possible reasons: * because you might be supporting old versions of Python (my offering runs on 1.5) * because the "<true_value> if <condition> else <false_value>" syntax gives you the screaming dry Edgar Britts * because you'd prefer not to have the overhead of a method lookup and method call
- Previous message (by thread): newbie question: if var1 == var2:
- Next message (by thread): newbie question: if var1 == var2:
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list