Export data from python to a txt file

Mark Lawrence breamoreboy at yahoo.co.uk
Fri Mar 29 13:58:34 EDT 2013


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[i]))
>      LDFile.write("\t")
>      LDFile.write(str(b[i]))

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




More information about the Python-list mailing list