Awkward format string

beginner zyzhu2000 at gmail.com
Wed Aug 1 12:42:58 EDT 2007


On Aug 1, 11:31 am, "Chris Mellon" <arka... at gmail.com> wrote:
> On 8/1/07, 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?
>
> You seem to be doing quite complicated things with your magical e
> tuple. Do you have some specific aversion to classes?

e is not complicated. It is a record that have 7 fields. In my program
a function outputs a list of tuples, each is of type e, and now I just
need to send them to a text file.

I have no problem using classes and I do use them everywhere. But
using classes does not solve my problem here. I will probably find
myself doing:

print  >>f, "%s\t%s\t%d\t%f\t%f\t%f\t%d" % (x.field1..strftime("%Y-%m-
%d"), x.field2..strftime("%Y-%m-%d"), x.field3, x.field4, x.field5,
x.field.6, x.field7)

This is also tedious and error-prone.






More information about the Python-list mailing list