【Python】模拟windows文件名排序

实现了一种模拟windows排序的python方法,其排序规则为:

  • 不处理浮点数
  • 特殊字符(如:&、$、# 等)排在数字和字母之前;
  • 数字优先于字母排序;
  • 数字是连着的整数,应该按照整数进行排序;
  • 小写字母排在大写字母前面;
  • 英文字符按字母表顺序排序;
def custom_sort_key(str_value):
    digital_res = ""
    digital_flag = False
    sort_list = []
    rank_value = []
    for c in str_value:
        c_ascii = ord(c)
        if c_ascii <= 57 and c_ascii >= 48:
            digital_flag = True
            digital_res += c
        else:
            if digital_flag:
                digital_res = int(digital_res)
                rank_value.append((1, digital_res))
                digital_res = ""
                digital_flag = False
            if c_ascii <= 47 or (c_ascii >= 58 and c_ascii <= 64) or (c_ascii >= 91 and c_ascii <= 96) or c_ascii >= 123:
                # special char
                rank_value.append((0, c_ascii))
            elif c_ascii >= 97 and c_ascii <= 122:
                rank_value.append((2, c_ascii))
            elif c_ascii >= 65 and c_ascii <= 90:
                rank_value.append((3, c_ascii))

            sort_list.extend(rank_value)
            rank_value = []

    return sort_list

root_path = r"./path"
image_names = os.path.listdir(root_path )
image_names = sorted(image_names, key=custom_sort_key)
print(image_names)

相关推荐

  1. Python模拟windows文件名排序

    2024-04-26 02:30:01       13 阅读
  2. python排序

    2024-04-26 02:30:01       38 阅读
  3. 单词分析---模拟排序

    2024-04-26 02:30:01       13 阅读
  4. Python 自动备份文件到远程目录(Windows适用)

    2024-04-26 02:30:01       11 阅读
  5. Python排序指南

    2024-04-26 02:30:01       35 阅读
  6. python归并排序

    2024-04-26 02:30:01       33 阅读
  7. python冒泡排序

    2024-04-26 02:30:01       40 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-26 02:30:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-26 02:30:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-26 02:30:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-26 02:30:01       20 阅读

热门阅读

  1. 2024 泛娱乐企业出海音视频选型攻略

    2024-04-26 02:30:01       26 阅读
  2. Migrations

    2024-04-26 02:30:01       12 阅读
  3. CSS图像样式

    2024-04-26 02:30:01       13 阅读
  4. Spring Gateway的入门概述简介

    2024-04-26 02:30:01       41 阅读
  5. eureka---各个server的地址配置不全导致服务找不到

    2024-04-26 02:30:01       33 阅读
  6. 人工智能对软件测试的影响

    2024-04-26 02:30:01       14 阅读