Reputation: 107
Hi folks I am inputting a filename.txt and producing multiple output files filename1.txt, filename2.txt and filename3.txt. To be more specific here is the input data in filename.txt:
Time(ms) Channel 1 Channel 2 Channel 3
0.0 4.5 3.6 125
1.0 3.0 3.4 98
2.0 100 3.0 59
3.0 23 45.9 2.1
4.0 34 123 35
5.0 2.1 222 98
filename1.txt should produce data of only columns Time and Channel 1 filename2.txt should produce data of only columns Time and Channel 2 filename3.txt should produce data of only columns Time and Channel 3
Source code:
with open('filename.txt', 'r') as input:
for i in range(1,4):
with open('filename%i.txt' %i, 'w') as output:
for line in input:
columns = line.strip().split()
for j in range(1,4):
output.write('{:10}{:10}\n'.format(columns[0], columns[j+1]))
Compiled I get text files filename1, filename2 and filename3 but only data in filename1. What happened to filename2 and filename3 data?
Upvotes: 3
Views: 12365
Reputation: 1213
Just a tip. After 2nd with
statement, add
input.seek(0)
could also do the trick.
Upvotes: 2
Reputation: 22007
You only read the input once, but tried to iterate over all its lines thrice. You could either open all 3 outputs and write to all them simultaneously, or open the input 3 times (once for each output file). The best approach will depend on your specific requirements (the size of the file, the number of output files, etc).
Opening 3 times produces cleaner code, but it might be less efficient:
for i in range(1,4):
with open('filename.txt', 'r') as input:
with open('filename%i.txt' %i, 'w') as output:
for line in input:
columns = line.strip().split()
output.write('{:10}{:10}\n'.format(columns[0], columns[i]))
A generalized solution for opening all output files at once would be better without the with
clause:
files = [open('filename%i.txt' %i, 'w') for i in range(1,4)]
with open('filename.txt', 'r') as input:
for line in input:
columns = line.strip().split()
for j in range(1,4):
files[j-1].write('{:10}{:10}\n'.format(columns[0], columns[j]))
for f in files:
f.close()
(you'd have to handle exceptions manually too, in this case)
Upvotes: 4
Reputation: 6177
for line in input
exhausts all the lines in the input
file. You have to reload the file and start over again at the beginning if you want to go through them again... or copy them to another list first.
Upvotes: 4