Re: Is there a better/simpler way to filter blank lines?

2008-11-05 Thread Marc 'BlackJack' Rintsch
On Wed, 05 Nov 2008 14:39:36 +1100, Ben Finney wrote: Marc 'BlackJack' Rintsch [EMAIL PROTECTED] writes: On Wed, 05 Nov 2008 13:18:27 +1100, Ben Finney wrote: Marc 'BlackJack' Rintsch [EMAIL PROTECTED] writes: Your example shows only that they're important for grouping the

Re: Is there a better/simpler way to filter blank lines?

2008-11-05 Thread Jorgen Grahn
On Tue, 04 Nov 2008 15:36:23 -0600, Larry Bates [EMAIL PROTECTED] wrote: [EMAIL PROTECTED] wrote: tmallen: I'm parsing some text files, and I want to strip blank lines in the process. Is there a simpler way to do this than what I have here? lines = filter(lambda line: len(line.strip()) 0,

Re: Is there a better/simpler way to filter blank lines?

2008-11-05 Thread tmallen
Why do I feel like the coding style in Lutz' Programming Python is very far from idiomatic Python? The content feels dated, and I find that most answers that I get for Python questions use a different style from the sort of code I see in this book. Thomas On Nov 5, 7:15 am, Jorgen Grahn [EMAIL

Re: Is there a better/simpler way to filter blank lines?

2008-11-05 Thread Lie
On Nov 5, 4:56 pm, Marc 'BlackJack' Rintsch [EMAIL PROTECTED] wrote: On Wed, 05 Nov 2008 14:39:36 +1100, Ben Finney wrote: Marc 'BlackJack' Rintsch [EMAIL PROTECTED] writes: On Wed, 05 Nov 2008 13:18:27 +1100, Ben Finney wrote: Marc 'BlackJack' Rintsch [EMAIL PROTECTED] writes: Your

Re: Is there a better/simpler way to filter blank lines?

2008-11-05 Thread Arnaud Delobelle
Lie [EMAIL PROTECTED] writes: What makes a generator expression is exp for var-or-tuple in exp. Parenthesis is generally required because without it, it's almost impossible to differentiate it with the surrounding. But it is not part of the formally required syntax. ... But *every*

Re: Is there a better/simpler way to filter blank lines?

2008-11-05 Thread Steven D'Aprano
On Wed, 05 Nov 2008 21:23:57 +, Arnaud Delobelle wrote: Lie [EMAIL PROTECTED] writes: What makes a generator expression is exp for var-or-tuple in exp. Parenthesis is generally required because without it, it's almost impossible to differentiate it with the surrounding. But it is not

Re: Is there a better/simpler way to filter blank lines?

2008-11-05 Thread Duncan Booth
Arnaud Delobelle wrote: Lie [EMAIL PROTECTED] writes: What makes a generator expression is exp for var-or-tuple in exp. Parenthesis is generally required because without it, it's almost impossible to differentiate it with the surrounding. But it is not part of the formally required syntax.

Re: Is there a better/simpler way to filter blank lines?

2008-11-05 Thread Miles
Ben Finney wrote: Falcolas writes: Using the surrounding parentheses creates a generator object No. Using the generator expression syntax creates a generator object. Parentheses are irrelevant to whether the expression is a generator expression. The parentheses merely group the expression

Re: Is there a better/simpler way to filter blank lines?

2008-11-05 Thread Ben Finney
Steven D'Aprano [EMAIL PROTECTED] writes: I'm surprised that nobody yet has RTFM: http://docs.python.org/reference/expressions.html generator_expression ::= ( expression genexpr_for ) ... The parentheses can be omitted on calls with only one argument. It's a fair cop. Thanks for setting

Is there a better/simpler way to filter blank lines?

2008-11-04 Thread tmallen
I'm parsing some text files, and I want to strip blank lines in the process. Is there a simpler way to do this than what I have here? lines = filter(lambda line: len(line.strip()) 0, lines) Thomas -- http://mail.python.org/mailman/listinfo/python-list

Re: Is there a better/simpler way to filter blank lines?

2008-11-04 Thread bearophileHUGS
tmallen: I'm parsing some text files, and I want to strip blank lines in the process. Is there a simpler way to do this than what I have here? lines = filter(lambda line: len(line.strip()) 0, lines) xlines = (line for line in open(filename) if line.strip()) Bye, bearophile --

Re: Is there a better/simpler way to filter blank lines?

2008-11-04 Thread Larry Bates
[EMAIL PROTECTED] wrote: tmallen: I'm parsing some text files, and I want to strip blank lines in the process. Is there a simpler way to do this than what I have here? lines = filter(lambda line: len(line.strip()) 0, lines) xlines = (line for line in open(filename) if line.strip()) Bye,

Re: Is there a better/simpler way to filter blank lines?

2008-11-04 Thread tmallen
On Nov 4, 4:30 pm, [EMAIL PROTECTED] wrote: tmallen: I'm parsing some text files, and I want to strip blank lines in the process. Is there a simpler way to do this than what I have here? lines = filter(lambda line: len(line.strip()) 0, lines) xlines = (line for line in open(filename) if

Re: Is there a better/simpler way to filter blank lines?

2008-11-04 Thread Steven D'Aprano
On Tue, 04 Nov 2008 13:27:00 -0800, tmallen wrote: I'm parsing some text files, and I want to strip blank lines in the process. Is there a simpler way to do this than what I have here? lines = filter(lambda line: len(line.strip()) 0, lines) Thomas lines = filter(lambda line:

Re: Is there a better/simpler way to filter blank lines?

2008-11-04 Thread Chris Rebert
On Tue, Nov 4, 2008 at 2:30 PM, tmallen [EMAIL PROTECTED] wrote: On Nov 4, 4:30 pm, [EMAIL PROTECTED] wrote: tmallen: I'm parsing some text files, and I want to strip blank lines in the process. Is there a simpler way to do this than what I have here? lines = filter(lambda line:

Re: Is there a better/simpler way to filter blank lines?

2008-11-04 Thread Ben Finney
Larry Bates [EMAIL PROTECTED] writes: [EMAIL PROTECTED] wrote: xlines = (line for line in open(filename) if line.strip()) Of if you want to filter/loop at the same time, or if you don't want all the lines in memory at the same time The above implementation creates a generator; so it, too,

Re: Is there a better/simpler way to filter blank lines?

2008-11-04 Thread Ben Finney
tmallen [EMAIL PROTECTED] writes: On Nov 4, 4:30 pm, [EMAIL PROTECTED] wrote: xlines = (line for line in open(filename) if line.strip()) I must be missing something: xlines = (line for line in open(new.data) if line.strip()) xlines generator object at 0x6b648 A generator

Re: Is there a better/simpler way to filter blank lines?

2008-11-04 Thread bearophileHUGS
tmallen I must be missing something: xlines = (line for line in open(new.data) if line.strip()) xlines generator object at 0x6b648 xlines.sort() Traceback (most recent call last): File stdin, line 1, in module AttributeError: 'generator' object has no attribute 'sort' What do you

Re: Is there a better/simpler way to filter blank lines?

2008-11-04 Thread Falcolas
On Nov 4, 3:30 pm, tmallen [EMAIL PROTECTED] wrote: On Nov 4, 4:30 pm, [EMAIL PROTECTED] wrote: tmallen: I'm parsing some text files, and I want to strip blank lines in the process. Is there a simpler way to do this than what I have here? lines = filter(lambda line: len(line.strip())

Re: Is there a better/simpler way to filter blank lines?

2008-11-04 Thread tmallen
Between this info and http://www.python.org/doc/2.5.2/tut/node11.html#SECTION0011100 , I'm starting to understand how I'll use generators (I've seen them mentioned before, but never used them knowingly). list_o_lines = [line for line in open(filename) if line.strip()] +1 for

Re: Is there a better/simpler way to filter blank lines?

2008-11-04 Thread Ben Finney
Falcolas [EMAIL PROTECTED] writes: Using the surrounding parentheses creates a generator object No. Using the generator expression syntax creates a generator object. Parentheses are irrelevant to whether the expression is a generator expression. The parentheses merely group the expression from

Re: Is there a better/simpler way to filter blank lines?

2008-11-04 Thread Steve Holden
tmallen wrote: On Nov 4, 4:30 pm, [EMAIL PROTECTED] wrote: tmallen: I'm parsing some text files, and I want to strip blank lines in the process. Is there a simpler way to do this than what I have here? lines = filter(lambda line: len(line.strip()) 0, lines) xlines = (line for line in

Re: Is there a better/simpler way to filter blank lines?

2008-11-04 Thread Marc 'BlackJack' Rintsch
On Wed, 05 Nov 2008 12:06:42 +1100, Ben Finney wrote: Falcolas [EMAIL PROTECTED] writes: Using the surrounding parentheses creates a generator object No. Using the generator expression syntax creates a generator object. Parentheses are irrelevant to whether the expression is a generator

Re: Is there a better/simpler way to filter blank lines?

2008-11-04 Thread Ben Finney
Marc 'BlackJack' Rintsch [EMAIL PROTECTED] writes: On Wed, 05 Nov 2008 12:06:42 +1100, Ben Finney wrote: Falcolas [EMAIL PROTECTED] writes: Using the surrounding parentheses creates a generator object No. Using the generator expression syntax creates a generator object.

Re: Is there a better/simpler way to filter blank lines?

2008-11-04 Thread Ben Finney
Steve Holden [EMAIL PROTECTED] writes: I think there'd be no advantage to a sort method on a generator, since theoretically the last item could be the first required in the sorted sequence Worse, generators don't necessarily *have* a finite set of items, and there's no way in general of

Re: Is there a better/simpler way to filter blank lines?

2008-11-04 Thread Steven D'Aprano
On Tue, 04 Nov 2008 20:25:09 -0500, Steve Holden wrote: I think there'd be no advantage to a sort method on a generator, since theoretically the last item could be the first required in the sorted sequence, so it's necessary to hold all items in memory to ensure the sort is correct. So

Re: Is there a better/simpler way to filter blank lines?

2008-11-04 Thread Marc 'BlackJack' Rintsch
On Wed, 05 Nov 2008 13:18:27 +1100, Ben Finney wrote: Marc 'BlackJack' Rintsch [EMAIL PROTECTED] writes: Your example shows only that they're important for grouping the expression from surrounding syntax. As I said. They are *not* important for making the expresison be a generator

Re: Is there a better/simpler way to filter blank lines?

2008-11-04 Thread Ben Finney
Marc 'BlackJack' Rintsch [EMAIL PROTECTED] writes: On Wed, 05 Nov 2008 13:18:27 +1100, Ben Finney wrote: Marc 'BlackJack' Rintsch [EMAIL PROTECTED] writes: Your example shows only that they're important for grouping the expression from surrounding syntax. As I said. They are