当我们使用os.listdir(rootDir)获得该文件夹下所有文件名称后,会发现其是乱序的,而在大数据处理过程中,我们往往希望可以按照顺序将样本输入进我们的系统。因此需要对包含文件名的list进行排序。
下面是对“字符串+数字”形式的文件名称进行排序的脚本,参考了网络上诸多写法。保存仅供学习交流!
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Feb 28 20:01:00 2019
基于字符串数字混合排序的Python脚本
@author: youxinlin
"""
def is_number(s):
try:
float(s)
return True
except ValueError:
pass
try:
import unicodedata
unicodedata.numeric(s)
return True
except (TypeError, ValueError):
pass
return False
def find_continuous_num(astr, c):
num = ''
try:
while not is_number(astr[c]) and c < len(astr):
c += 1
while is_number(astr[c]) and c < len(astr):
num += astr[c]
c += 1
e