From a file, i have taken a line, split the line into 5 columns using split(). But i have to write those columns as tab separated values in an output file.
Lets say that i have l[1], l[2], l[3], l[4], l[5]…a total of 5 entries. How can i achieve this using python? And also, i am not able to write l[1], l[2], l[3], l[4], l[5] values to an output file.
I tried both these codes, both not working(i am using python 2.6):
code 1:
with open('output', 'w'):
print l[1], l[2], l[3], l[4], l[5] > output
code 2:
with open('output', 'w') as outf:
outf.write(l[1], l[2], l[3], l[4], l[5])
You can use a parameter in the
withstatement representing the file you’re writing to. From there, use.write(). This assumes that everything inlis a string, otherwise you’d have to wrap all of them withstr().Alternatively, and more efficiently, you can use
.join():