Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Input a text file and write multiple output files in Python

Tags:

python

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?

like image 727
guiNachos Avatar asked Aug 22 '26 23:08

guiNachos


2 Answers

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.

like image 117
Steven T. Snyder Avatar answered Aug 25 '26 13:08

Steven T. Snyder


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)

like image 37
mgibsonbr Avatar answered Aug 25 '26 13:08

mgibsonbr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!