Export data from python to a txt file
Vincent Vande Vyvre
vincent.vandevyvre at swing.be
Fri Mar 29 14:00:58 EDT 2013
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[i]))
> LDFile.write("\t")
> LDFile.write(str(b[i]))
>
> 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>
More information about the Python-list
mailing list