ali
ali

Reputation: 51

export data into file in python

I am exporting my data from a list of lists.

when I run the following code

with open('out5.txt','w') as f :
    f.write ('\t'.join(z[0][0]))
    for i in rows:
        f.write ('\t'.join(i))

everything is in the same line but I want a file like this

id    name  Trans

ENS001 EGSB  TTP

EN02   EHGT  GFT

Upvotes: 0

Views: 73

Answers (2)

Alexander
Alexander

Reputation: 12785

You should add a newline characters \n

 f.write('\t'.join(i) + '\n')

I would do it like this :

from __future__ import print_function
with open('out5.txt','w') as f :
    print(*z[0][0], sep="\t", file=f, end="\n")
    for i in rows:
       print(*i, sep="\t", file=f, end="\n")

Upvotes: 1

Madela Abel
Madela Abel

Reputation: 21

You seem to be missing a newline after each print to file. You should try this

with open('out5.txt','w') as f :
    f.write ('\t'.join(z[0][0]))
    f.write ('\n')
    for i in rows:
        f.write ('\t'.join(i))
        f.write ('\n')

Upvotes: 0

Related Questions