write whitespace/tab to a text file

Discussion in 'Python' started by dirkheld, Oct 19, 2007.

  1. dirkheld

    dirkheld Guest

    Hi,

    I would l like to write some data to a text file. I want to write the
    data with whitespace or tabs in between so that I create tabular
    columns like in a spreadsheet. How can I do this in python.
    (btw, I'm new to python)

    names = ['John','Steve','asimov','fred','jim']
    ## output I would like in txt file : John Steve
    asimov fred jim

    f=open('/User/home/Documents/programming/python/test.txt','w')
    for x in range(len(names)):
    f.write(tags[x])
    f.close()
     
    dirkheld, Oct 19, 2007
    #1
    1. Advertising

  2. Le Fri, 19 Oct 2007 07:33:29 -0700, dirkheld a écrit :

    > Hi,
    >
    > I would l like to write some data to a text file. I want to write the
    > data with whitespace or tabs in between so that I create tabular columns
    > like in a spreadsheet. How can I do this in python. (btw, I'm new to
    > python)
    >
    > names = ['John','Steve','asimov','fred','jim'] ## output I would like in
    > txt file : John Steve asimov fred jim
    >
    > f=open('/User/home/Documents/programming/python/test.txt','w')
    > for x in range(len(names)):
    > f.write(tags[x])
    > f.close()

    Maybe :

    names = ["Sebastien", "Ana", "Elodie", "Mohamed", "Antoniavna"]
    maxlen = max(len(n) for n in names)
    linetowrite = ""
    for n in names:
    linetowrite += n.ljust(maxlen + 3, ' ')
    f = open('test.txt', 'w')
    f.writelines(linetowrite.strip(' ') + '\n')
    f.close()
     
    =?iso-8859-1?q?S=E9bastien?= Weber, Oct 19, 2007
    #2
    1. Advertising

  3. dirkheld wrote:

    > f=open('/User/home/Documents/programming/python/test.txt','w')
    > for x in range(len(names)):
    > f.write(tags[x])
    > f.close()


    Definitely consider the Python tutorial.

    Also, please provide working code examples. I don't think yours will
    work ;)

    names = ['John','Steve','asimov','fred','jim']
    f = open('/User/home/Documents/programming/python/test.txt','w')
    f.write('\t'.join(names))
    f.close()

    A better alternative could be the csv module.

    Regards,


    Björn


    --
    BOFH excuse #388:

    Bad user karma.
     
    Bjoern Schliessmann, Oct 19, 2007
    #3
  4. dirkheld

    marc wyburn Guest

    On Oct 19, 3:33 pm, dirkheld <> wrote:
    > Hi,
    >
    > I would l like to write some data to a text file. I want to write the
    > data with whitespace or tabs in between so that I create tabular
    > columns like in a spreadsheet. How can I do this in python.
    > (btw, I'm new to python)
    >
    > names = ['John','Steve','asimov','fred','jim']
    > ## output I would like in txt file : John Steve
    > asimov fred jim
    >
    > f=open('/User/home/Documents/programming/python/test.txt','w')
    > for x in range(len(names)):
    > f.write(tags[x])
    > f.close()


    I'm not sure exactly but you'll probably need to find out what the
    ASCII code is for a tab. Personally I would just write data straight
    into Excel using the win32 extensions. I'm sure the same can be
    achieved with OO on a Linux box.
     
    marc wyburn, Oct 19, 2007
    #4
  5. On 2007-10-19, marc wyburn <> wrote:

    >> I would l like to write some data to a text file. I want to write the
    >> data with whitespace or tabs in between so that I create tabular
    >> columns like in a spreadsheet. How can I do this in python.
    >> (btw, I'm new to python)
    >>
    >> names = ['John','Steve','asimov','fred','jim']
    >> ## output I would like in txt file : John Steve
    >> asimov fred jim
    >>
    >> f=open('/User/home/Documents/programming/python/test.txt','w')
    >> for x in range(len(names)):
    >> f.write(tags[x])
    >> f.close()

    >
    > I'm not sure exactly but you'll probably need to find out what the
    > ASCII code is for a tab.


    You don't need to know the ASCII code. Just use "\t":

    print "%s\t%s\t%s" % (1,"two",3)

    If you fixed column spacing with spaces instead of tabs:

    print "%-8s%-8s%-8s" % (1,"two",3)

    --
    Grant Edwards grante Yow! HAIR TONICS, please!!
    at
    visi.com
     
    Grant Edwards, Oct 19, 2007
    #5
    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. Oli Filth
    Replies:
    9
    Views:
    3,330
    Uncle Pirate
    Jan 17, 2005
  2. dirkheld
    Replies:
    1
    Views:
    319
    Tim Chase
    Oct 19, 2007
  3. dirkheld
    Replies:
    0
    Views:
    301
    dirkheld
    Oct 19, 2007
  4. Replies:
    10
    Views:
    745
    Eric Brunel
    Dec 16, 2008
  5. MRAB
    Replies:
    3
    Views:
    388
Loading...

Share This Page