Awkward format string
Gerard Flanagan
grflanagan at yahoo.co.uk
Wed Aug 1 17:28:39 EDT 2007
On Aug 1, 6:11 pm, beginner <zyzhu2... at gmail.com> 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?
>
YEARMONTHDAY = "%Y-%m-%d"
def strftime(dt):
return dt.strftime(YEARMONTHDAY)
def tostring(data):
return tuple(strftime(x) for x in data[:2]) + data[2:]
More information about the Python-list
mailing list