Steven D'Aprano wrote:
> On Tue, Apr 08, 2014 at 02:38:13PM -0600, Jared Nielsen wrote:
>> Hello,
>> Could someone explain why and how this list comprehension with strip()
>> works?
>>
>> f = open('file.txt')
>> t = [t for t in f.readlines() if t.strip()]
>> f.close()
>> print "".join(t)
>>
>> I
Thanks Danny!
That was an awesome explanation.
On Tue, Apr 8, 2014 at 7:05 PM, Danny Yoo wrote:
>
>> if line.strip()
>>
>> Is that stripping the line of white space at the same time that it is
>> testing it?
>>
>>
>
> Two features about Python:
>
> 1. Strings are immutable, so the above is com
Thank Danny,
That's much more clear.
But I still don't understand what's happening with:
if line.strip()
Is that stripping the line of white space at the same time that it is
testing it?
On Tue, Apr 8, 2014 at 3:44 PM, Danny Yoo wrote:
> > Could someone explain why and how this list comprehen
>
>
> if line.strip()
>
> Is that stripping the line of white space at the same time that it is
> testing it?
>
>
Two features about Python:
1. Strings are immutable, so the above is computing what a
whitespace-stripped line would look like. So that means that
'line.strip()' is doing just a com
On Tue, Apr 08, 2014 at 02:38:13PM -0600, Jared Nielsen wrote:
> Hello,
> Could someone explain why and how this list comprehension with strip()
> works?
>
> f = open('file.txt')
> t = [t for t in f.readlines() if t.strip()]
> f.close()
> print "".join(t)
>
> I had a very long file of strings fil
Jared Nielsen writes:
> I had a very long file of strings filled with blank lines I wanted to
> remove. I did some Googling and found the above code snippet
The code you found is one of several syntactic shortcuts in Python,
which allow creating a sequence directly from an expression in your
cod
Hi,
On 8 April 2014 22:38, Jared Nielsen wrote:
> Hello,
> Could someone explain why and how this list comprehension with strip()
> works?
>
> f = open('file.txt')
> t = [t for t in f.readlines() if t.strip()]
> f.close()
> print "".join(t)
>
> I had a very long file of strings filled with blank
> Could someone explain why and how this list comprehension with strip()
> works?
>
> f = open('file.txt')
> t = [t for t in f.readlines() if t.strip()]
> f.close()
> print "".join(t)
Hi Jared,
Let me rewrite this without the list comprehension, while preserving behavior.
#
Hello,
Could someone explain why and how this list comprehension with strip()
works?
f = open('file.txt')
t = [t for t in f.readlines() if t.strip()]
f.close()
print "".join(t)
I had a very long file of strings filled with blank lines I wanted to
remove. I did some Googling and found the above co