Home > OS >  Python: Create a file with a list of max values from multiple files
Python: Create a file with a list of max values from multiple files

Time:10-05

I have several files in a directory, consisting of the same size column of numbers in each file. Let's say 5 numbers. The new file I want to create using Python should also be a column of 5 numbers, but each number in this file is the maximum value in it's position from all the other files.

So, to demonstrate this, consider 3 files I have, which consists of 5 numbers as mentioned.

File 1: Column(1 2 3 4 5) File 2: Column(7 3 8 1 5) File 3: Column(2 3 6 8 3)

Then,

New File: Column(7 3 8 8 5)

Thanks

CodePudding user response:

If i understand your question, I would suggest creating a dictionary with 5 entries and for each file, update the maximal number (by the line index) for example:

def get_max_vector():
    all_files = ["file1.txt", "file2.txt"]
    lines_dict = {}
    for i in range(5):
        lines_dict[i] = -1
    for file_name in all_files:
        with open(file_name, "r") as fp:
            i = 0
            for line in fp:
                line = line.replace('\n', '')
                n = int(line)
                lines_dict[i] = max(lines_dict[i], n)
                i  = 1
    for i in range(5):
        print(f"max number in line {i} is {lines_dict[i]})

this example assumes each file is exactly 5 lines long, each line contains a positive number and that there is at least 1 file. you can add additional checks as you'd like, but this should be a good start for you

CodePudding user response:

This code makes no allowance for non-conforming input files but shows a strategy that you could consider using. It assumes that the relevant input files are in your current working directory and all have a .txt extension. The result will be written into the same directory but using a filename that does not have a .txt extension just in case you run it more than once. Assumes Python 3.8

import glob

cols = [0] * 5
for infile in glob.glob('*.txt'):
    with open(infile) as f:
        for i, v in enumerate(f.read().split()):
            if (_v := int(v)) > cols[i]:
                cols[i] = _v
with open('result.res', 'w') as out:
    print(*cols, file=out)
  • Related