Writing a nice formatted csv file

Discussion in 'Python' started by redcic, May 2, 2007.

  1. redcic

    redcic Guest

    Hi all,

    I use the csv module of Python to write a file. My code is of the
    form :

    cw = csv.writer(open("out.txt", "wb"))
    cw.writerow([1,2,3])
    cw.writerow([10,20,30])

    And i get an out.txt file looking like:
    1,2,3
    10,20,30

    Whereas what I'd like to get is:
    1, 2, 3,
    10, 20, 30

    which is more readable.

    Can anybody help me to do so ?

    Thanks,

    Cédric
     
    redcic, May 2, 2007
    #1
    1. Advertising

  2. redcic

    7stud Guest

    On May 2, 8:14 am, redcic <> wrote:
    > Hi all,
    >
    > I use the csv module of Python to write a file. My code is of the
    > form :
    >
    > cw = csv.writer(open("out.txt", "wb"))
    > cw.writerow([1,2,3])
    > cw.writerow([10,20,30])
    >
    > And i get an out.txt file looking like:
    > 1,2,3
    > 10,20,30
    >
    > Whereas what I'd like to get is:
    > 1, 2, 3,
    > 10, 20, 30
    >
    > which is more readable.
    >
    > Can anybody help me to do so ?
    >
    > Thanks,
    >
    > Cédric


    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.
     
    7stud, May 2, 2007
    #2
    1. Advertising

  3. On 2 May 2007 07:14:04 -0700, redcic <> wrote:
    > And i get an out.txt file looking like:
    > 1,2,3
    > 10,20,30
    > Whereas what I'd like to get is:
    > 1, 2, 3,
    > 10, 20, 30
    > which is more readable.


    The idea behind csv module is to produce and read csv files that are
    "machine readable" rather than "human readable", so I think you should
    write the file "by hand" to take into account those whitespace.



    --
    Sebastián Bassi (ã‚»ãƒã‚¹ãƒ†ã‚£ã‚¢ãƒ³)
    Diplomado en Ciencia y Tecnología.
    GPG Fingerprint: 9470 0980 620D ABFC BE63 A4A4 A3DE C97D 8422 D43D
    Club de la razón (www.clubdelarazon.org)
     
    Sebastian Bassi, May 2, 2007
    #3
  4. redcic

    redcic Guest

    Well then how can I format a file ?

    Thanks for your help,

    Cédric

    On 2 mai, 16:26, 7stud <> wrote:
    > On May 2, 8:14 am, redcic <> wrote:
    >
    >
    >
    > > Hi all,

    >
    > > I use the csv module of Python to write a file. My code is of the
    > > form :

    >
    > > cw = csv.writer(open("out.txt", "wb"))
    > > cw.writerow([1,2,3])
    > > cw.writerow([10,20,30])

    >
    > > And i get an out.txt file looking like:
    > > 1,2,3
    > > 10,20,30

    >
    > > Whereas what I'd like to get is:
    > > 1, 2, 3,
    > > 10, 20, 30

    >
    > > which is more readable.

    >
    > > Can anybody help me to do so ?

    >
    > > Thanks,

    >
    > > Cédric

    >
    > 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.
     
    redcic, May 2, 2007
    #4
  5. redcic

    Guest

    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>
     
    , May 2, 2007
    #5
  6. redcic

    Jon Clements Guest

    On 2 May, 15:14, redcic <> wrote:
    > Hi all,
    >
    > I use the csv module of Python to write a file. My code is of the
    > form :
    >
    > cw = csv.writer(open("out.txt", "wb"))
    > cw.writerow([1,2,3])
    > cw.writerow([10,20,30])
    >
    > And i get an out.txt file looking like:
    > 1,2,3
    > 10,20,30
    >
    > Whereas what I'd like to get is:
    > 1, 2, 3,
    > 10, 20, 30
    >
    > which is more readable.
    >
    > Can anybody help me to do so ?


    How about pre-formatting the columns before hand before using
    something like:

    # List of formatting to apply to each column (change this to suit)
    format = ['%03d', '%10s', '%10s']

    data = [1, 10, 100]
    print [fmt % d for fmt,d in zip(format,data)]

    Results in: ['001', ' 10', ' 100']

    Then write that using the CSV module.

    hth

    Jon.
     
    Jon Clements, May 2, 2007
    #6
  7. redcic

    Dave Borne Guest

    > Whereas what I'd like to get is:
    > 1, 2, 3,
    > 10, 20, 30


    (without trying this myself first...)
    You might try setting up a csv dialect with a delimiter of ',\t' then
    using a reader that can set tab stops for display.

    -Dave
     
    Dave Borne, May 2, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ram Laxman
    Replies:
    22
    Views:
    938
    Programmer Dude
    Feb 11, 2004
  2. Ram Laxman

    How to Parse a CSV formatted text file

    Ram Laxman, Feb 7, 2004, in forum: C Programming
    Replies:
    22
    Views:
    1,145
    Programmer Dude
    Feb 11, 2004
  3. Uri
    Replies:
    4
    Views:
    316
    Fuzzyman
    May 11, 2004
  4. Replies:
    3
    Views:
    602
    Dennis Lee Bieber
    Sep 2, 2005
  5. Tintin92
    Replies:
    1
    Views:
    1,806
    Andrew Thompson
    Feb 14, 2007
Loading...

Share This Page