如何分组相关的.tif文件?

时间:2015-05-21 20:44:35

标签: python tiff itertools

我正在尝试对与.tif图像关联的相关文件进行分组。您可以从列表中看到每个组有7个相关文件。我正在寻找一种方法来分组这些文件,以便我可以通过shutil.move()将它们移动到各种文件夹中。

以下脚本是我根据this answer尝试失败的原因。结果与输入files列表相同。

我如何调整它来执行我追求的分组?

import os, itertools

files = ['F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511759_sw.tfw',
       'F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511759_sw.tif',
       'F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511759_sw.tif.aux.xml',
       'F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511759_sw.tif.ovr',
       'F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511759_sw.tif.vat.cpg',
       'F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511759_sw.tif.vat.dbf',
       'F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511759_sw.tif.xml',
       'F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511760_sw.tfw',
       'F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511760_sw.tif',
       'F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511760_sw.tif.aux.xml',
       'F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511760_sw.tif.ovr',
       'F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511760_sw.tif.vat.cpg',
       'F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511760_sw.tif.vat.dbf',
       'F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511760_sw.tif.xml']

test = sorted(files)
grouped = [list(g) for _, g in itertools.groupby(test, lambda x: x.split('_')[1])]

预期输出:

[['F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511759_sw.tfw',
           'F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511759_sw.tif',
           'F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511759_sw.tif.aux.xml',
           'F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511759_sw.tif.ovr',
           'F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511759_sw.tif.vat.cpg',
           'F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511759_sw.tif.vat.dbf',
           'F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511759_sw.tif.xml'],
           ['F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511760_sw.tfw',
           'F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511760_sw.tif',
           'F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511760_sw.tif.aux.xml',
           'F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511760_sw.tif.ovr',
           'F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511760_sw.tif.vat.cpg',
           'F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511760_sw.tif.vat.dbf',
           'F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511760_sw.tif.xml']]

1 个答案:

答案 0 :(得分:1)

关闭!拆分列表上的索引是一次性的,应该是2.

尝试

grouped = [list(g) for _, g in itertools.groupby(test, lambda x: x.split('_')[2])]