Author - StudySection Post Views - 61 views
Python

How to sort a list in AlphaNumeric Order Python

While reading multiple files from the folder I was faced with a problem.

I have a directory with .json and other files in it, the .json all have filenames with numbers in them. Some may have additional strings in the filename.

For example, 01.json Or it could be File_03.json, etc

If I tried to sort directory using “Sorted Keyword”
import os,glob,sys
FileDirectoryPath ="D:\\Machine\\api\\files"
OutputFilePath = FileDirectoryPath +os.sep+'*'+os.sep+'output'+os.sep+'*'
fileList = [] fileList = glob.glob(OutputFilePath)
print(sorted(fileList))#sort numerically in ascending order
sys.exit();

What I get using Python is this

[‘0.json’, ‘1.json’, ’10.json’, ’11.json’, ’12.json’, ’13.json’, ’14.json’, ’15.json’, ’16.json’, ’17.json’, ’18.json’, ’19.json’, ‘2.json’, ’20.json’, ’21.json’, ’22.json’, ’23.json’, ’24.json’, ’25.json’, ’26.json’, ’27.json’, ’28.json’, ’29.json’, ‘3.json’, ’30.json’, ’31.json’, ’32.json’, ’33.json’, ’34.json’, ’35.json’, ’36.json’, ’37.json’, ‘4.json’, ‘5.json’, ‘6.json’, ‘7.json’, ‘8.json’, ‘9.json’, file_1.json’, ‘file_10.json’, ‘file_11.json’, ‘file_12.json’, ‘file_2.json’, ‘file_21.json’, ‘file_3.json’]

But this is not the actual output that I want.

I want output like this order
['0.json', '1.json', '2.json', '3.json'........file_1.json',file_2.json',file_3.json',.......]
I want to process files in ascending order. So for the above output, you need to use the python module natsort this. This module will sort your file name in alphabetical order as well as numeric order.

By using pip you can install this module in python
To install this module you need to run this command in the command line.
pip install natsort
After that import module into a file and use it as shown below
import os,glob,sys
from natsort import natsorted, ns
FileDirectoryPath ="D:\\Machine\\api\\files"
OutputFilePath = FileDirectoryPath +os.sep+'*'+os.sep+'output'+os.sep+'*'
fileList = [] fileList = glob.glob(OutputFilePath)
fileList = natsorted(fileList, key=lambda y: y.lower())#sort alphanumeric in ascending order
print(fileList)
sys.exit();

Then you will get output in this format
[‘0.json’, ‘1.json’, ‘2.json’, ‘3.json’, ‘4.json’, ‘5.json’, ‘6.json’, ‘7.json’, ‘8.json’, ‘9.json’, ’10.json’, ’11.json’, ’12.json’, ’13.json’, ’14.json’, ’15.json’, ’16.json’, ’17.json’, ’18.json’, ’19.json’, ’20.json’, ’21.json’, ’22.json’, ’23.json’, ’24.json’, ’25.json’, ’26.json’, ’27.json’, ’28.json’, ’29.json’, ’30.json’, ’31.json’, ’32.json’, ’33.json’, ’34.json’, ’35.json’, ’36.json’, ’37.json’, ‘file_1.json’, ‘file_2.json’, ‘file_3.json’, ‘file_10.json’, ‘file_11.json’, ‘file_12.json’, ‘file_21.json’]

StudySection gives an opportunity to beginners and experts in .NET framework to go through StudySection’s .NET Certification Exam and get a .NET certification for enhancement of career in programming. If you have knowledge of the .NET framework then you can get a certificate through an online exam at StudySection.

Leave a Reply

Your email address will not be published. Required fields are marked *