3

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?

1

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, just starting with Python. Love it better than C.
4

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)

2 Comments

Thanks this helps. I just realized I am only reading once but writing thrice. Is the "j" variable has some meaning?
nope. My mistake, when adapting your code I left the "j" there. Answer updated
2

Just a tip. After 2nd with statement, add

input.seek(0)

could also do the trick.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.