I have tab delimited data that I am exporting a select few columns into another file. I have:
a b c d
1 2 3 4
5 6 7 8
9 10 11 12
and I get:
b, d
b, d
2, 4
b, d
2, 4
6, 8
b, d
2, 4
6, 8
10, 12
......
I want:
b, d
2, 4
6, 8
10, 12
My code is
f=open('data.txt', 'r')
f1=open('newdata.txt','w')
t=[]
for line in f.readlines():
line =line.split('\t')
t.append('%s,%s\n' %(line[0], line[3]))
f1.writelines(t)
What am I doing wrong??? Why is it repeating?
PLease help
Thanks!!
The indentation is wrong so you are writing the entire array t on every iteration instead of only at the end. Change it to this:
Alternatively you could write the lines one at a time instead of waiting until the end, then you don’t need the array
tat all.