Writing a nice formatted csv file

dustin at v.igoro.us dustin at v.igoro.us
Wed May 2 10:38:57 EDT 2007


On Wed, May 02, 2007 at 07:28:32AM -0700, redcic wrote:
> Well then how can I format a file ?

for row in rows:
    print "".join([ "%-6s" % ("%d," % cell) for cell in row ])

The "%-6s" formats each column to be no less than six characters long;
the "%d," formats the number with a comma after it.

This won't be quite what you want, since you've comma-aligned all of the
fields after the first, but it should be readable.

> > > Whereas what I'd like to get is:
> > > 1,    2,    3,
> > > 10,  20,   30
> >
> > > which is more readable.
<snip>
> >
> > cvs files are constructed for efficient processing not formatting so
> > that you can read them easier.  If you want a formatted file, then
> > construct one.
<snip>



More information about the Python-list mailing list