对os.listdir文件进行排序Python [英] Sort os.listdir files Python

查看:744
本文介绍了对os.listdir文件进行排序Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果使用以下命名约定下载了存储在文件中的数年数据,则为year_day.dat。例如,名为2014_1.dat的文件具有2014年1月1日的数据。我需要读取按日,2014_1.dat,2014_2.dat,2014_3.dat排序的这些数据文件,直到年底。在文件夹中,当我在目录中创建文件列表时,它们被列在那个有序的BUT中,它们被重新排序为2014_1.dat,2014_10.dat,2014_100.dat,2014_101.dat ... 2014.199.dat,2014_2.dat。
我想我需要使用排序功能,但是如何强制它按日排序列出的文件,以便我可以继续处理它们?
以下是到目前为止的代码:

If have downloaded several years of data stored in files with the following naming convention, year_day.dat. For example, the file named 2014_1.dat has the data for January 1, 2014. I need to read these data files ordered by day, 2014_1.dat, 2014_2.dat, 2014_3.dat until the end of the year. In the folder they are listed in that ordered BUT when I create a list of the files in the directory they are reordered 2014_1.dat, 2014_10.dat, 2014_100.dat, 2014_101.dat...2014.199.dat, 2014_2.dat. I think I need to use a sort function but how do I force it to sort the listed files by day so I can continue processing them? Here's the code so far:

import sys, os, gzip, fileinput, collections
# Set the input/output directories
wrkDir = "C:/LJBTemp"
inDir = wrkDir + "/Input"
outDir = wrkDir + "/Output"
# here we go
inList = os.listdir(inDir)  # List all the files in the 'Input' directory
print inList  #print to screen reveals 2014_1.dat.gz followed by 2014_10.dat.gz NOT    2014_2.dat.gz HELP
d = {}
for fileName in inList:     # Step through each input file 
    readFileName = inDir + "/" + fileName

    with gzip.open(readFileName, 'r') as f: #call built in utility to unzip file for reading
      for line in f:
          city, long, lat, elev, temp = line.split() #create dictionary
          d.setdefault(city, []).append(temp) #populate dictionary with city and associated temp data from each input file
          collections.OrderedDict(sorted(d.items(), key=lambda d: d[0])) # QUESTION? why doesn't this work
          #now collect and write to output file
outFileName = outDir + "/" + "1981_maxT.dat" #create output file in output directory with .dat extension
with open(outFileName, 'w') as f:
     for city, values in d.items():
        f.write('{} {}\n'.format(city, ' '.join(values)))

print "All done!!"
raw_input("Press <enter>") # this keeps the window open until you press "enter"


推荐答案

如果您不介意使用第三方库,可以使用 natsort 库,专为这种情况而设计。

If you don't mind using third party libraries, you can use the natsort library, which was designed for exactly this situation.

import natsort
inList = natsort.natsorted(os.listdir(inDir))

这应该照顾所有的数字排序无需担心细节。

This should take care of all the numerical sorting without having to worry about the details.

您还可以使用 ns.PATH 选项进行排序算法路径感知:

You can also use the ns.PATH option to make the sorting algorithm path-aware:

from natsort import natsorted, ns
inList = natsorted(os.listdir(inDir), alg=ns.PATH)






完全披露,我是 natsort 作者。

这篇关于对os.listdir文件进行排序Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆