【问题标题】:Sorting the files according to filename in python在python中根据文件名对文件进行排序
【发布时间】:2020-04-01 18:34:40
【问题描述】:

使用整数文件名对文件进行排序。

  1. 我需要使用文件名中的整数根据文件名升序Python中的文件进行排序. python3中的sort函数将文件名排序为1,11,12,13,......,19,2,21 ,22.....等等。
  2. 有些文件的文件名有例外,例如,有些文件被命名为 1a.htm、3a.htm、4a.htm。我想忽略这些文件,并且不想将这些文件包含在我最终排序的文件列表中。 The files and folders I want to sort

正常排序问题:


    import bs4
    from bs4 import BeautifulSoup
    import os,glob

    root='data_sample_gnc/'
    for path, subdirs, files in os.walk(root):
        for file in sorted(files):
            if(file.endswith('.htm')):
                print(file)

输出 1.htm 10.htm 11.htm 12.htm 13.htm 14.htm 15.htm 16.htm 17.htm 18.htm 19.htm 1a.htm 2.htm 20.htm 21.htm 22.htm 23.htm 24.htm 25.htm 26.htm 27.htm 28.htm 29.htm 3.htm 30.htm

异常问题:


    import bs4
    from bs4 import BeautifulSoup
    import os,glob

    root='data_sample_gnc/'
    for path, subdirs, files in os.walk(root):
        sorted_files=sorted(files, key=lambda x: int(x.split('.')[0]))
        for file in sorted_files:
            print(file)

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-45-2881098be594> in <module>
      5 root='data_sample_gnc/'
      6 for path, subdirs, files in os.walk(root):
----> 7     sorted_files=sorted(files, key=lambda x: int(x.split('.')[0]))
      8     for file in sorted_files:
      9         print(file)

<ipython-input-45-2881098be594> in <lambda>(x)
      5 root='data_sample_gnc/'
      6 for path, subdirs, files in os.walk(root):
----> 7     sorted_files=sorted(files, key=lambda x: int(x.split('.')[0]))
      8     for file in sorted_files:
      9         print(file)

ValueError: invalid literal for int() with base 10: '1a'

提前感谢您的帮助。 :) xD

【问题讨论】:

  • 分割点并使用if条件isalnum作为第一个元素

标签: python python-3.x file sorting


【解决方案1】:

过滤非数字然后排序。

示例(使用文件字符串)

files = "1a.htm, 1b.htm, 1.htm 10.htm 11.htm 12.htm 13.htm 14.htm 15.htm 16.htm 17.htm 18.htm 19.htm 1a.htm 2.htm 20.htm 21.htm 22.htm 23.htm 24.htm 25.htm 26.htm 27.htm 28.htm 29.htm 3.htm 30.htm"

sorted_files = sorted(filter(lambda x: x.split('.')[0].isnumeric(), files.split()), key= lambda y: int(y.split('.')[0]))

print(sorted_files)

在上面的 files.split() 中将字符串转换为文件列表(例如)

输出

['1.htm', '2.htm', '3.htm', '10.htm', '11.htm', '12.htm', '13.htm', '14.htm', '15.htm', '16.h.htm'tm', '17.htm', '18.htm', '19.htm', '20.htm', '21.htm', '22.htm', '23.htm', '24.htm', '25.htm'7.htm, '26.htm', '27.htm', '28.htm', '29.htm', '30.htm']

发布示例(文件列表)

Posting 从以下位置获取文件列表:

for path, subdirs, files in os.walk(root):

文件已经 type ,所以我们不执行拆分来获取列表。

 sorted_files = sorted(filter(lambda x: x.split('.')[0].isnumeric(), files), key= lambda y: int(y.split('.')[0]))

# Above changed from files.split() to just files

【讨论】:

  • 你在输出 '1315.htm' 中有一个类型 编辑:实际上我不确定你是如何从那个输入中得到输出的
  • @AlbertoPoljak--谢谢。不知道为什么早期的运行会产生这种情况。我相信现在输出是正确的。
  • @DarrylG 我有一个文件列表,而不是您作为输入的文件名字符串。我得到错误 AttributeError: 'list' object has no attribute 'split'。
  • @antonyjames--更新了我为您的案例发布的帖子。基本上,由于您有一个文件列表,因此您在排序表达式中使用 files 而不是 files.split()。
猜你喜欢
  • 2017-08-14
  • 1970-01-01
  • 1970-01-01
  • 2016-04-08
  • 2018-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-20
相关资源
最近更新 更多