Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #42259
| References | <eb74d8be-d3e6-4f65-8a5b-f6a87c1a6af8@googlegroups.com> |
|---|---|
| Date | 2013-03-29 13:52 -0400 |
| Subject | Re: Export data from python to a txt file |
| From | Jason Swails <jason.swails@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3955.1364579536.2939.python-list@python.org> (permalink) |
[Multipart message — attachments visible in raw view] - view raw
On Fri, Mar 29, 2013 at 1:33 PM, Ana Dionísio <anadionisio257@gmail.com>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[i]))
> LDFile.write("\t")
> LDFile.write(str(b[i]))
>
> 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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Export data from python to a txt file Ana Dionísio <anadionisio257@gmail.com> - 2013-03-29 10:33 -0700 Re: Export data from python to a txt file Jason Swails <jason.swails@gmail.com> - 2013-03-29 13:52 -0400 Re: Export data from python to a txt file rusi <rustompmody@gmail.com> - 2013-03-29 10:55 -0700 Re: Export data from python to a txt file Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-03-29 17:58 +0000 Re: Export data from python to a txt file Vincent Vande Vyvre <vincent.vandevyvre@swing.be> - 2013-03-29 19:00 +0100 Re: Export data from python to a txt file Peter Otten <__peter__@web.de> - 2013-03-29 19:11 +0100
csiph-web