file io (lagged values) newbie question

Steven D'Aprano steve at REMOVE.THIS.cybersource.com.au
Tue Feb 20 17:35:43 EST 2007


On Tue, 20 Feb 2007 22:03:43 +0100, Bruno Desthuilliers wrote:

[snip]

>>>And that's not cruft?
>> 
>> 
>> No. Why do you think it is crufty?
> 
> Because it is ?

*shrug* 

"Is not."
"Is too."
"Is not."

Why is converting a list of integers to a string all at once more crufty
than converting them one at a time? Because you have to remove the
delimiters? That's no bigger a deal than adding spaces or newlines, and
if removing the commas worries you, change the output format to separate
the numbers with comma instead of space. 



>> Would it be less crufty if I wrote it as a cryptic one liner without
>> comments?
>> 
>> f.write(str(data)[1:-1].replace(',', '') + '\n')
> 
> Nope. It's still a WTF.
> 
>> Okay, it depends on the string conversion of a list.
> 
> Nope. It depends on the *representation* of a list.

No, that would be repr(data) instead of str(data). An insignificant detail
for lists, but potentially very different for other data types.


>> But that's not going
>> to change any time soon.
> 
>> 
>>>Try this: f.write(' '.join(str(x) for x in data) + '\n')
>> 
>> 
>> That will only work in Python 2.5 or better.
> 
> Really ?

[snip demonstration]

Well, I'll be hornswaggled. Who snuck generator expressions into 2.4? I
thought they were only in 2.5 and up.



-- 
Steven.




More information about the Python-list mailing list