简体   繁体   中英

Input a text file and write multiple output files in 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?

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.

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)

Just a tip. After 2nd with statement, add

input.seek(0)

could also do the trick.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM