I don´t use python that often and I´m kind of new to it. I have a lot af layers in a single folder and I need to merge all the layers that starts and ends with the same character. I´ve done it manually before but it takes too much time to select the layers one by one through the merge tool and I have tried to use python to do it semiautomated, with Listfeatureclasses and wildcards and then continue to merge them, but it was also timeconsuming. It also seems like a waste of time to do it "my way" every time there are new updates.
I have tried to find a solution to my problem online and I searched through forums, but I´ve found nothing yet (maybe I´m searching for the wrong things). Below is a printscreen of a small part of my folder, so there are many more layers starting with other characters than e and h. Can I somehow with python group them and merge those that have the same filetype and characters (not middle characters)?
If this question already exist with an answer please let me know
Solved! Go to Solution.
Hi Johanna,
Here is a sample that may work:
import arcpy from arcpy import env env.workspace = r"E:\Temp\Python\Data.gdb" #create list of feature clases fcList = [fc for fc in arcpy.ListFeatureClasses("*")] char = [] #append unique first and last character groupings for fc in fcList: if fc[0] + "_*_" + fc[-1] not in char: char.append(fc[0] + "_*_" + fc[-1]) #merge results for wildCard in char: name = wildCard.split("_*_")[0] + wildCard.split("_*_")[-1] + "_merge" newfcList = [fc for fc in arcpy.ListFeatureClasses(wildCard)] arcpy.Merge_management(newfcList, name)
Hi Johanna,
Here is a sample that may work:
import arcpy from arcpy import env env.workspace = r"E:\Temp\Python\Data.gdb" #create list of feature clases fcList = [fc for fc in arcpy.ListFeatureClasses("*")] char = [] #append unique first and last character groupings for fc in fcList: if fc[0] + "_*_" + fc[-1] not in char: char.append(fc[0] + "_*_" + fc[-1]) #merge results for wildCard in char: name = wildCard.split("_*_")[0] + wildCard.split("_*_")[-1] + "_merge" newfcList = [fc for fc in arcpy.ListFeatureClasses(wildCard)] arcpy.Merge_management(newfcList, name)
Thank you so much, this worked!