file io (lagged values) newbie question

Steven D'Aprano steve at REMOVE.THIS.cybersource.com.au
Tue Feb 20 11:33:46 EST 2007


On Tue, 20 Feb 2007 07:08:02 -0800, John Machin wrote:

>> def write_series(data, f):
>>     """Write a time series data to file f.
>>
>>     data should be a list of integers.
>>     f should be an already opened file-like object.
>>     """
>>     # Convert data into a string for writing.
>>     s = str(data)
>>     s = s[1:-1]  # strip the leading and trailing [] delimiters
>>     s = s.replace(',', '') # delete the commas
>>     # Now write it to the file object
>>     f.write(s)
>>     f.write('\n')
> 
> And that's not cruft?

No. Why do you think it is crufty?

Would it be less crufty if I wrote it as a cryptic one liner without
comments?

f.write(str(data)[1:-1].replace(',', '') + '\n')

Okay, it depends on the string conversion of a list. 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.


-- 
Steven.




More information about the Python-list mailing list