Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: 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; 'url:launchpad': 0.05; 'subject:file': 0.07; 'f.close()': 0.09; 'python': 0.11; "'r',": 0.16; "'w')": 0.16; 'format:': 0.16; 'from:addr:swing.be': 0.16; 'from:addr:vincent.vandevyvre': 0.16; 'from:name:vincent vande vyvre': 0.16; 'help?': 0.16; 'message-id:@swing.be': 0.16; 'oqapy': 0.16; 'paqager': 0.16; 'received:mobistar.be': 0.16; 'subject:txt': 0.16; 'url:oqapy': 0.16; 'url:paqager': 0.16; 'url:qarte': 0.16; 'v.v.': 0.16; '\xe9crit': 0.16; 'subject:python': 0.16; '>>>': 0.22; 'print': 0.22; 'header:User- Agent:1': 0.23; 'code:': 0.26; 'header:In-Reply-To:1': 0.27; "doesn't": 0.30; 'txt': 0.31; 'file': 0.32; 'lists': 0.32; 'thanks!': 0.32; 'subject:from': 0.34; "can't": 0.35; 'something': 0.35; 'but': 0.35; 'subject:data': 0.36; 'next': 0.36; 'to:addr :python-list': 0.38; 'to:addr:python.org': 0.39; 'skip:x 10': 0.40; 'more': 0.64; 'export': 0.74; 'ana': 0.84; 'sta': 0.84 Date: Fri, 29 Mar 2013 19:00:58 +0100 From: Vincent Vande Vyvre User-Agent: Mozilla/5.0 (X11; Linux i686; rv:17.0) Gecko/20130308 Thunderbird/17.0.4 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Export data from python to a txt file References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 59 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1364580518 news.xs4all.nl 6849 [2001:888:2000:d::a6]:49993 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:42263 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 . Qarte . PaQager