D
D
Denis Petrenko2021-05-20 10:34:02
Python
Denis Petrenko, 2021-05-20 10:34:02

How to parse and sort filenames?

The input data is a list of names always in the format "file675.txt" there are many of them. When you try to sort, of course, you get something like:
file1.txt
file100.txt
file2.txt
How can I fix this and how can I parse the names and sort the list correctly for further output?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
MinTnt, 2021-05-20
@Termoslik

a = ['file1.txt', 'file10.txt', 'file2.txt']
print(sorted(a, key=lambda x: int(x[4:-4])))

Code explanation. sorted() for strings - sorts them alphabetically. That is, as it sorts AB AB BA, so it is for the numbers in the line, first what starts with 1, then what starts with 2, and so on.
So for correct sorting, the supplied number-string must be converted from the string format to a number. And there, as we know the standard form of strings, we simply use the cut [4:-4]

M
Mikhail Krostelev, 2021-05-20
@twistfire92

it is necessary for each name to make a certain correspondence with the number that is embedded in it.
Create a list of these matches and sort by number.
If you quickly throw it, then something like this will come out:

names = ['file1.txt',
'file100.txt',
'file2.txt']

new_list = []

for name in names:
  number = int(name.replace('file', '').replace('.txt', ''))
  new_list.append((name, number))

sorted_files = [element[0] for element in sorted(new_list, key=lambda x: x[1])]

print(sorted_files) # ['file1.txt', 'file2.txt', 'file100.txt']

If you need clarifications to the code - write

S
Stalker_RED, 2021-05-20
@Stalker_RED

https://pypi.org/project/natsort/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question