Python Forum
Human Sorting (natsort) does not work [SOLVED]
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Human Sorting (natsort) does not work [SOLVED]
#1
Hello everybody,

With a batch-script I export mutliple text-files to a specific folder where I want to merge them into one.
Therefore I used the type-command in windows cmd which worked fine. But after comparing the files I noticed that the order is wrong. I found out the CMD sorts them differently (file_1,file_10,file_11,file_2,file_3,file_33) instead of how I'm doing it (file_1,file_2,file_3,file_10,file_11,file_33).

This is my script, which does not sort files after human sorting:

#!/usr/bin/env python3

#Imports
import sys
from pathlib import Path
from natsort import natsorted, humansorted

#String Configuration
outputs = Path(r'C:\System\Mediainfo\Outputs\tmp\bts')
search = "*Behind the Scenes #*.txt"
sorted_outputs = humansorted(outputs.glob(search))
lines = []

#Sorting
for path in sorted_outputs:
    if path.is_file():
        print(path)
        with path.open() as fd:
            for line in fd:
                print(line)
                lines.append(line.strip())

#Output Configuration
with open('C:\\System\\Mediainfo\\Outputs\\master\\bts.txt', 'w') as f:
    f.write('\n'.join(lines))

#End
sys.exit()
Reply
#2
For sorting pathlib object add key=str.
Test.
from pathlib import Path
from natsort import natsorted, humansorted
from pprint import pprint

outputs = Path(r'G:\div_code\answer\sort_test')
search = "*.txt"
sorted_outputs = humansorted(outputs.glob(search), key=str)
pprint(sorted_outputs)
Output:
[WindowsPath('G:/div_code/answer/sort_test/file_1.txt'), WindowsPath('G:/div_code/answer/sort_test/file_2.txt'), WindowsPath('G:/div_code/answer/sort_test/file_3.txt'), WindowsPath('G:/div_code/answer/sort_test/file_10.txt'), WindowsPath('G:/div_code/answer/sort_test/file_11.txt'), WindowsPath('G:/div_code/answer/sort_test/file_33.txt')]
Reply
#3
(Jul-04-2022, 09:56 AM)snippsat Wrote: For sorting pathlib object add key=str.
Test.
from pathlib import Path
from natsort import natsorted, humansorted
from pprint import pprint

outputs = Path(r'G:\div_code\answer\sort_test')
search = "*.txt"
sorted_outputs = humansorted(outputs.glob(search), key=str)
pprint(sorted_outputs)
Output:
[WindowsPath('G:/div_code/answer/sort_test/file_1.txt'), WindowsPath('G:/div_code/answer/sort_test/file_2.txt'), WindowsPath('G:/div_code/answer/sort_test/file_3.txt'), WindowsPath('G:/div_code/answer/sort_test/file_10.txt'), WindowsPath('G:/div_code/answer/sort_test/file_11.txt'), WindowsPath('G:/div_code/answer/sort_test/file_33.txt')]

Ah thank you. That worked fine. Thumbs Up
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sorting and Merging text-files [SOLVED] AlphaInc 10 4,916 Aug-20-2021, 05:42 PM
Last Post: snippsat
  sorting alphanumeric values in a human way idiotonboarding 3 2,636 Jan-22-2021, 05:57 PM
Last Post: idiotonboarding
  Sorting a copied list is also sorting the original list ? SN_YAZER 3 3,081 Apr-11-2019, 05:10 PM
Last Post: SN_YAZER
  How to convert Python crawled Bing web page content to human readable? dalaludidu 4 3,430 Sep-02-2018, 04:15 PM
Last Post: dalaludidu
  Batch job from epoch to human time jheeman 6 4,550 Feb-27-2018, 10:53 PM
Last Post: jheeman
  Time Difference in Epoch Microseconds then convert to human readable firesh 4 11,649 Feb-27-2018, 09:08 AM
Last Post: firesh

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020