使用Python对文件列表进行排序
发布时间:2020-12-20 12:42:16 所属栏目:Python 来源:网络整理
导读:我需要将一个装满pdfs的文件夹合并到一个文件中.但是,它们必须按特定顺序组合.文件名的示例是: WR_Mapbook__1.pdf WR_Mapbook__1a.pdf WR_Mapbook__2.pdf WR_Mapbook__2a.pdf WR_Mapbook__3.pdf WR_Mapbook__3a.pdf etc... 它们在Windows资源管理器中排序的
我需要将一个装满pdfs的文件夹合并到一个文件中.但是,它们必须按特定顺序组合.文件名的示例是:
WR_Mapbook__1.pdf WR_Mapbook__1a.pdf WR_Mapbook__2.pdf WR_Mapbook__2a.pdf WR_Mapbook__3.pdf WR_Mapbook__3a.pdf etc... 它们在Windows资源管理器中排序的方式是我需要将它们添加到单个文件中的方式.但是我的脚本首先添加所有“a”文件,然后添加没有“a”的文件.为什么这样做?如何对其进行排序以便以我想要的方式添加文件? 请参阅下面的代码.谢谢! from pyPdf import PdfFileWriter,PdfFileReader import glob outputLoc = "K:testpdf_output" output = PdfFileWriter() pdfList = glob.glob(r"K:testlidar_MB_ALL*.pdf") pdfList.sort print pdfList for pdf in pdfList: print pdf input1 = PdfFileReader(file(pdf,"rb")) output.addPage(input1.getPage(0)) # finally,write "output" to document-output.pdf outputStream = file(outputLoc + "WR_Imagery_LiDar_Mapbook.pdf","wb") output.write(outputStream) print ("adding " + pdf) outputStream.close() 解决方法
你需要的是实现
“Natural Order String Comparison”.
希望有人已经这样做并分享了它. 编辑:这是在Python中执行此操作的强力示例. import re digits = re.compile(r'(d+)') def tokenize(filename): return tuple(int(token) if match else token for token,match in ((fragment,digits.search(fragment)) for fragment in digits.split(filename))) # Now you can sort your PDF file names like so: pdfList.sort(key=tokenize) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |