Reputation: 437
I have two folders and each folder includes five text files (input1.txt
, input2.txt
, input3.txt
, input4.txt
, input5.txt
). I am trying to read the forth column from each text file and write it to an output file (output.txt
). I want to have two columns in the output file (first and second columns would be for 5 numbers from the text files in the folder1 and folder2, respectively) but my script writes only one column to output file. Do you know how I can fix my code to get two columns in the output file?
Obtained output:
0.12
0.17
0.54
0.79
1.15
2.24
3.51
4.01
5.08
6.22
Desired output:
0.12 2.24
0.17 3.51
0.54 4.01
0.79 5.08
1.15 6.22
Script:
# !/usr/bin/env python3
import os
import re
with open("output.txt", mode='w') as out:
folders = ["folder1", "folder2"]
for folder in folders:
os.chdir(folder)
search_word = 'The result is:'
for i in range(1, 6):
with open('input'+ str(int(i)) + '.txt', mode='r') as infile:
for lines in infile:
if search_word in lines:
columns = lines.split()
out.write( "%.2f\n" % (float(columns[4])))
os.chdir("..")
Upvotes: 1
Views: 772
Reputation: 87084
You get a single column of output because you are working first on the files in one folder, then the files in the second folder. No attempt is made to collate the data from each file.
Instead, collect the data as you read it from the files into a dictionary that uses the folder name for a key and the data extracted from the files in that folder as a list of values.
defaultdict
is handy for this as it simplifies the hassle of creating a dictionary of lists. After processing all of the files you can easily combine the collected data and write it to a file.
Here is some code that should work for you:
import os
import re
from collections import defaultdict
with open("output.txt", mode='w') as out:
folders = ["folder1", "folder2"]
data = defaultdict(list) # create a dictionary with lists for values
for folder in folders:
os.chdir(folder)
search_word = 'The result is:'
for i in range(1, 6):
with open('input'+ str(int(i)) + '.txt', mode='r') as infile:
for lines in infile:
if search_word in lines:
columns = lines.split()
data[folder].append(float(columns[4])) # add to the list for this folder
os.chdir("..")
# combine the data in the lists and write to output file
for v1, v2 in zip(*data.values()):
out.write(f"{v1:.2f} {v2:.2f}\n")
Upvotes: 2