There are a list of files in my directory, and now I aim to save the content to a database. Prior to that however, I need to sort them in the right order.
The current ordering is as follows:
['8.txt', '8-0.txt', '8-1.txt', '8-2.txt', '8-0-0.txt', '8-0-1.txt',
'8-0-2.txt', '8-0-3.txt', '8-1-0.txt', '8-2-0.txt', '8-2-1.txt']
And I wish to have them order as follows:
['8.txt', '8-0.txt', '8-0-0.txt', '8-0-1.txt', '8-0-2.txt', '8-0-3.txt',
'8-1.txt', '8-1-0.txt', '8-2.txt', '8-2-0.txt', '8-2-1.txt']
Basically, think of these files as posts, comments and replies.
The first file '8.txt' without any dash is the original post. Followed by that we have a group of comments, eg '8-0.txt', '8-1.txt', etc, ie one dash in the file names. Finally, for each comment, there could potentially be some replies, which has the name format, '8-2-0.txt', '8-2-1.txt', (2 dashes).
While I know the brute force way can definitely do this kind of ordering, I wonder if there are any Pythonic ways (eg some lambda functions in the function for sorting)
For now we can assume there are only up to 2 dashes in the filenames, ie no more than 3 levels of hierarchy.
Any help will be very much appreciated!
The difficulty is sorting things correctly that are similar to
8-1-12.txt # simply removing non digits ==> 8112
8-11-2.txt # simply removing non digits ==> 8112 as well
Easy solution is to leverage tuple
- sorting:
f = ['8.txt', '8-0.txt', '8-0-0.txt', '8-0-1.txt', '8-0-2.txt', '8-0-3.txt',
'8-1.txt', '8-1-0.txt', '8-2.txt', '8-2-0.txt', '8-2-1.txt','8-12-0.txt',
'8-1-12.txt', '8-11-2.txt']
def to_tuple(text):
"""Extract all numbers from file as tuple (8,1,3) ... etc."""
return tuple(map(int, text.split(".")[0].split("-") ))
f.sort(key = to_tuple)
print(f)
Output:
['8.txt', '8-0.txt', '8-0-0.txt', '8-0-1.txt', '8-0-2.txt', '8-0-3.txt',
'8-1.txt', '8-1-0.txt', '8-1-12.txt',
'8-2.txt', '8-2-0.txt', '8-2-1.txt',
'8-11-2.txt', '8-12-0.txt']
If your names contain things that are not int-conversible you need to use try: except:
and refine the to_tuple()
function to handle your names correctly.
自然分类包括那些以及其他。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.