How to sort numbers from two different txt files then save them as one txt file

Question:

Could anyone help please.
I have the following task:

  • Create a new Python file in this folder called ​combined.py 
  • Create a text file called ​numbers1.txt that contains Integers which are sorted from smallest to largest. 

  • Create another text file called ​numbers2.txt which also contains Integers that are sorted from smallest to largest. 

  • Write the numbers from both files to a third file called ​all_numbers.txt 

  • All the numbers in ​all_numbers.txt should be sorted from smallest to largest.

the 2 txt files are as follows:

numbers1:

20
10
30
50
40
60

then:

numbers2:

999
80
150
101
100

The following code below takes the two txt files and saves them as one file correctly. I’m just having some trouble sorting the integers from lowest to highest. Any help will be greatly appreciated! Thank you!

filenames = ['numbers1.txt', 'numbers2.txt']
with open('all_numbers.txt', 'w') as outfile:
    for a in filenames:
        with open(a) as infile:
            outfile.write(infile.read() + "n")
print("Your file is saved under all_numbers.txt")
Asked By: user12739323

||

Answers:

It sounds like a simple merge would be best, we’ll use sorted to keep things simple for now.

First, you should modularize your code a bit so that the logic of each section is clear. Let’s extract the numbers from each file into a list using a function:

def load_numbers(filepath):
    with open(filepath, 'r') as file:
        return [int(n) for n in file.readlines()]

We can now invoke it to load our numbers into a list:

first_numbers = load_numbers('numbers1.txt')
second_numbers = load_numbers('numbers2.txt')

Now we need a way to merge these two lists and ensure they’re sorted. Using Python’s sorted we can do:

sorted_numbers = sorted(first_numbers + second_numbers)

Joe’s answer has a great way to extend this to multiple lists using sum.

To write this to file we can do something similar to what we did with reading:

with open('all_numbers.txt', 'w') as file:
    file.writelines(sorted_numbers)

Altogether:

def load_numbers(filepath):
    with open(filepath, 'r') as file:
        return [int(n) for n in file.readlines()]


if __name__ = '__main__':
    first_numbers = load_numbers('numbers1.txt')
    second_numbers = load_numbers('numbers2.txt')

    sorted_numbers = sorted(first_numbers + second_numbers)

    with open('all_numbers.txt', 'w') as file:
        file.writelines(sorted_numbers)
Answered By: shayaan

Currently you are writing the contents of each input file to the output as soon as you read them (outfile.write(infile.read() + "n")). To process them, I would suggest you read them first into lists, then work from there.

To create a list of integers from each file, there are numerous methods. One is to read the entire file to a string with .read(), strip any excess whitespace and newlines with .strip() and split on newlines. You can then use a list comprehension or a map or some equivalent methodology to convert this list of strings of numbers to a list of integers.

Then you need to combine these two lists and sort them. There are many algorithms for this. Seeing as your task has not specified, you could just use the built-in sorted() function or the list method .sort(). This would have to operate on a list consisting of the two lists concatenated together. To concatenate two lists in Python, we can just add them ([1, 2] + [3, 4] == [1, 2, 3, 4]).

Therefore, your final solution could look something like:

filenames = ['numbers1.txt', 'numbers2.txt']
num_lists = [[int(x) for x in open(f).read().strip().split('n')] 
             for f in filenames]
with open('all_numbers.txt', 'w') as outfile:
    outfile.write('n'.join(str(x) for x in sorted(sum(num_lists, []))) + 'n')

print('Your file is saved under all_numbers.txt')

Note that sum(numbers_list, []) is equivalent to numbers_list[0] + numbers_list[1], but is better as your solution will now work for any number of input files. 🙂

Test

$ echo '20
> 10
> 30
> 50
> 40
> 60' > numbers1.txt
$ echo '999
> 80
> 150
> 101
> 100' > numbers2.txt
$ python -q
>>> filenames = ['numbers1.txt', 'numbers2.txt']
>>> num_lists = [[int(x) for x in open(f).read().strip().split('n')] 
...              for f in filenames]
>>> with open('all_numbers.txt', 'w') as outfile:
...     outfile.write('n'.join(str(x) for x in sorted(sum(num_lists, []))) + 'n')
... 
37
>>> print('Your file is saved under all_numbers.txt')
Your file is saved under all_numbers.txt
>>> 
$ cat all_numbers.txt 
10
20
30
40
50
60
80
100
101
150
999
Answered By: Joe Iddon

you forgot to close your text file at the end.

Answered By: user20944862
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.