Awkward format string

Ian Clark iclark at mail.ewu.edu
Wed Aug 1 12:54:22 EDT 2007


beginner wrote:
> Hi,
> 
> In order to print out the contents of a list, sometimes I have to use
> very awkward constructions. For example, I have to convert the
> datetime.datetime type to string first, construct a new list, and then
> send it to print. The following is an example.
> 
> 	x=(e[0].strftime("%Y-%m-%d"), e[1].strftime("%Y-%m-%d"))+e[2:]
> 	print  >>f, "%s\t%s\t%d\t%f\t%f\t%f\t%d" % x
> 
> e is a tuple. x is my new tuple.
> 
> Does anyone know better ways of handling this?
> 
> Thanks,
> Geoffrey
> 

>>> import datetime
>>> old_tuple = (
...     datetime.datetime(2007, 8, 1),
...     datetime.datetime(2007, 8, 2),
...     1,
...     2.0,
...     3.0,
...     4
... )
>>> first_date = old_tuple[0].strftime('%Y-%m-%d')
>>> second_date = old_tuple[1].strftime('%Y-%m-%d')
>>> new_tuple = (first_date, second_date) + old_tuple[2:]
>>> print '\t'.join(str(i) for i in new_tuple)
2007-08-01      2007-08-02      1       2.0     3.0     4

Without more information that's the best I can think of.

Ian




More information about the Python-list mailing list