Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #42259
| Path | csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <jason.swails@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.001 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'element': 0.07; 'interpreter.': 0.07; 'subject:file': 0.07; 'variables': 0.07; '[1,': 0.09; 'assuming': 0.09; 'executes': 0.09; 'objects,': 0.09; 'strings.': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; "'r',": 0.16; "'w')": 0.16; "['a',": 0.16; 'format:': 0.16; 'help?': 0.16; 'newline,': 0.16; 'subject:txt': 0.16; 'tab,': 0.16; '\xa0this': 0.16; 'subject:python': 0.16; 'followed': 0.16; 'wrote:': 0.18; 'obviously': 0.18; "skip:' 30": 0.19; 'work,': 0.20; '8bit%:5': 0.22; 'code,': 0.22; 'email addr:gmail.com>': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'skip:\xa0 20': 0.24; '\xa0so': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'this:': 0.26; 'code:': 0.26; 'header:In-Reply-To:1': 0.27; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; "skip:' 10": 0.31; '(although': 0.31; 'equivalent.': 0.31; 'prints': 0.31; 'txt': 0.31; 'file': 0.32; 'lists': 0.32; 'another': 0.32; 'fri,': 0.33; 'subject:from': 0.34; "can't": 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'subject:data': 0.36; 'doing': 0.36; 'next': 0.36; 'skip:o 20': 0.38; '8bit%:4': 0.38; 'jason': 0.38; 'skip:- 10': 0.38; 'pm,': 0.38; 'rather': 0.38; '8bit%:6': 0.40; 'skip:x 10': 0.40; 'letters': 0.60; 'most': 0.60; 'first': 0.61; 'different': 0.65; 'to:addr:gmail.com': 0.65; 'mar': 0.68; 'export': 0.74; 'to:charset:iso-8859-1': 0.74; 'walk': 0.74; 'ana': 0.84; '2013': 0.98 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:cc:content-type; bh=5aJhucFhQRv1/G3g1KtbHjczQ8GWKGsjORs4chlrN5w=; b=pq7zLCG7A+1VuYya0tswrFV3oO9fVfYf596VGCcncW3OX87i05hRx2fqyrW8q4eCyu 9kYUmWjg1IBG2k0RJSm43SjVBSZjXarhEBSeShx3ip6uz/ZgA9/5vbg+bCyRMq7j8JM8 7OnUavsSjslAYYE+XO458ZXZDQyqAZsBQTjtgBH/qgHsJCUBP1XI834vqMLmv42606GA LYx1KYIlxVDSREhPtc9ilgSBNQo04ZuepbDECmzsaD3+Rjg5yHMvLJpOAvFyvCUjv0gu B1LuXfz12D1MxXkB1vHs98MLI2oWHKBjsF+aEG7FNQ3K6oQ7UykGRNPWukHDx8XivqZp UPww== |
| MIME-Version | 1.0 |
| X-Received | by 10.50.153.232 with SMTP id vj8mr293896igb.2.1364579532710; Fri, 29 Mar 2013 10:52:12 -0700 (PDT) |
| In-Reply-To | <eb74d8be-d3e6-4f65-8a5b-f6a87c1a6af8@googlegroups.com> |
| References | <eb74d8be-d3e6-4f65-8a5b-f6a87c1a6af8@googlegroups.com> |
| Date | Fri, 29 Mar 2013 13:52:12 -0400 |
| Subject | Re: Export data from python to a txt file |
| From | Jason Swails <jason.swails@gmail.com> |
| To | Ana Dionísio <anadionisio257@gmail.com> |
| Content-Type | multipart/alternative; boundary=e89a8f23551f50f6fd04d913ee5b |
| Cc | python-list@python.org |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3955.1364579536.2939.python-list@python.org> (permalink) |
| Lines | 174 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1364579536 news.xs4all.nl 6893 [2001:888:2000:d::a6]:38990 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:42259 |
Show key headers only | View raw
[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