简体   繁体   English

输入文本文件并使用Python编写多个输出文件

[英]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. 嗨,大家好,我正在输入filename.txt并生成多个输出文件filename1.txt,filename2.txt和filename3.txt。 To be more specific here is the input data in filename.txt: 更具体地说,这里是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 filename1.txt应该只产生时间和通道1列的数据filename2.txt应该只产生时间和通道2列的数据filename3.txt应该只产生时间和通道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. 编译后,我得到了文本文件filename1,filename2和filename3,但是只有filename1中的数据。 What happened to filename2 and filename3 data? filename2和filename3数据发生了什么?

for line in input exhausts all the lines in the input file. for line in input的行会耗尽input文件中的所有行。 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). 您可以打开所有3个输出并同时对其进行写入,也可以打开输入3次(每个输出文件一次)。 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: 打开3次会产生更清晰的代码,但效率可能较低:

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: 如果没有with子句,一次打开所有输出文件的通用解决方案会更好:

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 在第二个with语句之后,添加

input.seek(0)

could also do the trick. 也可以做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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