Could anyone help please. I have the following task:
numbers1.txt
that contains Integers which are sorted from smallest to largest.numbers2.txt
which also contains Integers that are sorted from smallest to largest.all_numbers.txt
the 2 txt files are as follows:
numbers1.txt
:
20
10
30
50
40
60
numbers2.txt
:
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")
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. :)
$ 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