python listdir排序_对os.listdir文件进行排序Python

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 ") # this keeps the window open until you press "enter"

解决方案

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.

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)

Full disclosure, I am the natsort author.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值