I am trying to merge files starting with same 2 characters as below;
- 11111 - merge (output name : 11.shp)
- 11112 - merge (output name : 11.shp)
- 11113 - merge (output name : 11.shp)
- 22112 - merge (output name : 22.shp)
- 22153 - merge (output name : 22.shp)
But my python script' result merge together if "11" is including in the filenames(like 4)). I only want 11***.shp(1)~3)) but A Script merges 11.shp (merge 1~4 as 11.shp)
========================================================================
import sys, os, arcpy, fnmatch
reload(sys)
arcpy.env.workspace = r"F:\a"
workspace = arcpy.env.workspace
outdir = r"F:\b"
List = []
for dirpath, dirnames, filenames in os.walk(workspace, topdown=True):
for filename in filenames:
List.append(os.path.join(dirpath, filename))
break
patterns = set([os.path.basename(fl)[:2] for fl in List])
for pattern in patterns:
group = [fl for fl in List if fnmatch.fnmatchcase(fl, '*' + pattern + '*.shp')]
output = os.path.join(outdir, pattern)
arcpy.Merge_management(group, output)
========================================================================
How can I fix it?
1 Answer 1
I like collections.defaultdict(list):
...to group a sequence of key-value pairs into a dictionary of lists
Example:
import arcpy, os
from collections import defaultdict as dd
folder = r'C:\GIS\data\testdata\merge'
out_folder = r'C:\GIS\data\testdata\outfolder'
d = dd(list)
for item in os.listdir(folder):
if os.path.isfile(os.path.join(folder,item)) and file.endswith('.shp'):
d[item[:2]].append(os.path.join(folder, item))
#Execute code above then print(d) to make sure its ok before merging
for group, mergelist in d.items():
#print(group, mergelist)
arcpy.Merge_management(inputs=mergelist, output=os.path.join(out_folder, 'Merge_{}.shp'.format(group)))
If you want to search subdirectories aswell use os.walk like you tried, example:
import os
from collections import defaultdict as dd
folder_main = r'C:\GIS\data\testdata'
d = dd(list)
for root, folder, files in os.walk(folder_main):
for file in files:
if os.path.isfile(os.path.join(root, file)) and file.endswith('.shp'):
print(file)
d[file[:2]].append(os.path.join(root, file))
-
I am quite new to python script. How can I set the output folder and shp file ( #and file.endswith('.shp'))?user127580– user1275802020年08月20日 08:20:18 +00:00Commented Aug 20, 2020 at 8:20
-
-
1You are briliant !! Last one more question please,,, If input folder has sub-directories, can it be include as input (Above script seems sub-dir files excluded)user127580– user1275802020年08月20日 08:59:02 +00:00Commented Aug 20, 2020 at 8:59
-
:) . Yes use os.walk like you tried instead of os.listdirBera– Bera2020年08月20日 09:04:23 +00:00Commented Aug 20, 2020 at 9:04
-
1I did it perfectly ! THANK you for your super-kind answer !user127580– user1275802020年08月20日 09:31:37 +00:00Commented Aug 20, 2020 at 9:31