Export data from python to a txt file

Discussion in 'Python' started by Ana Dionísio, Mar 29, 2013.

  1. Hello!!!

    I have this lists a=[1,3,5,6,10], b=[a,t,q,r,s] and I need to export it to a txt file and I can't use csv.

    And I want the next format:

    a 1 3 5 6 10
    b a t q r s

    I already have this code:

    "f = open("test.txt", 'w')
    f.write("a")
    f.write("\n")
    f.write("b")
    f.write("\n")

    for i in xrange(len(a)):
    LDFile.write("\t")
    LDFile.write(str(a))
    LDFile.write("\t")
    LDFile.write(str(b))

    f.close()"

    But it doesn't have the format I want. Can you help?

    Thanks!
     
    Ana Dionísio, Mar 29, 2013
    #1
    1. Advertising

  2. Ana Dionísio

    Jason Swails Guest

    On Fri, Mar 29, 2013 at 1:33 PM, Ana Dionísio <>wrote:

    > Hello!!!
    >
    > I have this lists a=[1,3,5,6,10], b=[a,t,q,r,s] and I need to export it to
    > a txt file and I can't use csv.
    >


    It would help if you showed exactly what you had in your program or in the
    Python interpreter. For instance

    a = [1, 3, 5, 6, 10]
    b = [a, t, q, r, s]

    won't work, since it interprets the letters in the b-list as variables
    rather than strings. It would need to be

    b = ['a', 't', 'q', 'r', 's']

    or something equivalent. Assuming you had that part correct:


    > And I want the next format:
    >
    > a 1 3 5 6 10
    > b a t q r s
    >
    > I already have this code:
    >
    > "f = open("test.txt", 'w')
    > f.write("a")
    > f.write("\n")
    > f.write("b")
    > f.write("\n")
    >
    > for i in xrange(len(a)):
    > LDFile.write("\t")
    > LDFile.write(str(a))
    > LDFile.write("\t")
    > LDFile.write(str(b))
    >
    > f.close()"
    >
    > But it doesn't have the format I want. Can you help?
    >


    Walk through exactly what the computer is doing here. First it prints a,
    then a newline, then b, then a newline:

    --begin--
    a
    b
    --end--

    Then your for loop executes (notice LDFile and f are different file
    objects, so they won't write to the same location). This will print a tab,
    followed by the i'th element of a, followed by another tab, followed by the
    i'th element of b. So this:

    a 1 3 5 6 10
    b a t q r s

    --begin-- [tab] 1 [tab] a [tab] 3 [tab] t [tab] 5 [tab] q [tab]
    6 [tab] r [tab] 10 [tab] s [tab]--end--

    So the final file will look something like this:

    a
    b
    1 a 3 t 5 q 6 r 10 s


    which is obviously not what you want. The following code will do what you
    want (although there are ways of doing this in less code, this is the most
    straightforward):

    f.write('a\t')
    for element in a:
    f.write(str(element) + '\t')
    f.write('\n')
    f.write('b\t')
    for element in b:
    f.write(str(element) + '\t')
    f.write('\n')

    HTH,
    Jason
     
    Jason Swails, Mar 29, 2013
    #2
    1. Advertising

  3. Ana Dionísio

    rusi Guest

    On Mar 29, 10:33 pm, Ana Dionísio <> wrote:
    > Hello!!!
    >
    > I have this lists a=[1,3,5,6,10], b=[a,t,q,r,s] and I need to export it to a txt file and I can't use csv.
    >
    > And I want the next format:
    >
    > a 1 3 5 6 10
    > b a t q r s
    >
    > I already have this code:
    >
    > "f = open("test.txt", 'w')
    >  f.write("a")
    >  f.write("\n")
    >  f.write("b")
    >  f.write("\n")
    >
    >  for i in xrange(len(a)):
    >     LDFile.write("\t")
    >     LDFile.write(str(a))
    >     LDFile.write("\t")
    >     LDFile.write(str(b))
    >
    >  f.close()"
    >
    > But it doesn't have the format I want. Can you help?
    >
    > Thanks!


    Dunno about LDFile. [Neither does google]

    You can try:
    >>> a=[1,3,7,10,100]
    >>> "a " + " ".join(map(str,a))

    'a 1 3 7 10 100'
     
    rusi, Mar 29, 2013
    #3
  4. On 29/03/2013 17:33, Ana Dionísio wrote:
    > Hello!!!
    >
    > I have this lists a=[1,3,5,6,10], b=[a,t,q,r,s] and I need to export it to a txt file and I can't use csv.
    >
    > And I want the next format:
    >
    > a 1 3 5 6 10
    > b a t q r s
    >
    > I already have this code:
    >
    > "f = open("test.txt", 'w')
    > f.write("a")


    You'll have to write your data here.

    > f.write("\n")


    And here.

    > f.write("b")
    > f.write("\n")
    >
    > for i in xrange(len(a)):
    > LDFile.write("\t")
    > LDFile.write(str(a))
    > LDFile.write("\t")
    > LDFile.write(str(b))


    You rarely need a loop like this in Python.
    for x in a:
    doSomething
    is the usual way of writing this. But you don't need this anyway. Just
    use the string join method as you can't use csv, which tells me it's
    homework, so I'll leave you to it :)

    >
    > f.close()"
    >
    > But it doesn't have the format I want. Can you help?
    >
    > Thanks!
    >


    --
    If you're using GoogleCrap™ please read this
    http://wiki.python.org/moin/GoogleGroupsPython.

    Mark Lawrence
     
    Mark Lawrence, Mar 29, 2013
    #4
  5. Le 29/03/13 18:33, Ana Dionísio a écrit :
    > Hello!!!
    >
    > I have this lists a=[1,3,5,6,10], b=[a,t,q,r,s] and I need to export it to a txt file and I can't use csv.
    >
    > And I want the next format:
    >
    > a 1 3 5 6 10
    > b a t q r s
    >
    > I already have this code:
    >
    > "f = open("test.txt", 'w')
    > f.write("a")
    > f.write("\n")
    > f.write("b")
    > f.write("\n")
    >
    > for i in xrange(len(a)):
    > LDFile.write("\t")
    > LDFile.write(str(a))
    > LDFile.write("\t")
    > LDFile.write(str(b))
    >
    > f.close()"
    >
    > But it doesn't have the format I want. Can you help?
    >
    > Thanks!
    >
    >

    Something like that:

    Python 2.6.5 (r265:79063, Oct 1 2012, 22:07:21)
    [GCC 4.4.3] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> a=[1,3,5,6,10]
    >>> b=['a', 't', 'q', 'r', 's']
    >>> sta = " ".join([str(i) for i in a])
    >>> stb = " ".join(b)
    >>> txt = "a " + sta + '\nb ' + stb
    >>> f = open('test.txt', 'w')
    >>> f.write(txt)
    >>> f.close()
    >>> f = open('test.txt', 'r')
    >>> for line in f:

    .... print line
    ....
    a 1 3 5 6 10

    b a t q r s
    >>>




    --
    Vincent V.V.
    Oqapy <https://launchpad.net/oqapy> . Qarte
    <https://launchpad.net/qarte> . PaQager <https://launchpad.net/paqager>
     
    Vincent Vande Vyvre, Mar 29, 2013
    #5
  6. Ana Dionísio

    Peter Otten Guest

    Ana Dionísio wrote:

    > I have this lists a=[1,3,5,6,10], b=[a,t,q,r,s] and I need to export it to
    > a txt file and I can't use csv.


    What do you mean by "can't use csv"?

    - You cannot get it to work with the csv module
    - You are not allowed to use csv by your instructor
    - Something else.

    > And I want the next format:
    >
    > a 1 3 5 6 10
    > b a t q r s
    >
    > I already have this code:
    >
    > "f = open("test.txt", 'w')
    > f.write("a")
    > f.write("\n")
    > f.write("b")
    > f.write("\n")
    >
    > for i in xrange(len(a)):
    > LDFile.write("\t")
    > LDFile.write(str(a))
    > LDFile.write("\t")
    > LDFile.write(str(b))
    >
    > f.close()"
    >
    > But it doesn't have the format I want. Can you help?
    >
    > Thanks!
     
    Peter Otten, Mar 29, 2013
    #6
    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. =?Utf-8?B?RGF2aWQgVmFsbGU=?=

    Invalid export DLL or export format

    =?Utf-8?B?RGF2aWQgVmFsbGU=?=, Oct 29, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    3,666
    =?Utf-8?B?RGF2aWQgVmFsbGU=?=
    Oct 29, 2003
  2. Nicolas

    export datagrid to txt file (help!!)

    Nicolas, Jan 16, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    497
    Nicolas
    Jan 16, 2004
  3. Sameen
    Replies:
    2
    Views:
    477
    Victor Bazarov
    Aug 29, 2005
  4. Jochen Brenzlinger
    Replies:
    7
    Views:
    5,859
    Roedy Green
    Sep 15, 2011
  5. John Berman

    Export to Excel or txt File

    John Berman, Apr 10, 2004, in forum: ASP General
    Replies:
    3
    Views:
    184
    Aaron Bertrand [MVP]
    Apr 11, 2004
Loading...

Share This Page