Select to view content in your preferred language

add a list in arcpy.MakeFeatureLayer_management

1545
13
Jump to solution
09-19-2022 05:53 AM
danielROUFFART1
New Contributor II

Hi! i ty to make

 

 

 

 

output = r'C:\Temp\test.gdb\BAV1'
target = r'C:\download

fcs2 = [a, b, c, d]
# Set the workspace
arcpy.env.workspace = ready
for x in range(len(fcs2)):
    arcpy.MakeFeatureLayer_management(fcs2, output)

arcpy.SelectLayerByAttribute_management(output, 'NEW_SELECTION', "city = 'Paris'")

arcpy.CopyFeatures_management(output, target)

but i've got an error message, can you help me about my script for put my list please

Tags (1)
0 Kudos
13 Replies
by Anonymous User
Not applicable

You could try using the addfielddeliminators to ensure that the SQL is formatted correctly and check for the field 'commune' in each fetureclass.

for fc in fcs2:
    flds = [f.name for f in arcpy.ListFields(fc)]
    if 'commune' in flds:
        # build the query using field deliminators 
        whereclause = f"""{arcpy.AddFieldDelimiters(fc, 'commune')} = 'Perpignan'"""
        arcpy.MakeFeatureLayer_management(fc, fc + '_lyr', whereclause)
        # continue with copying
    else:
        print (f'commune not found in {fc}')

 

0 Kudos
danielROUFFART1
New Contributor II

ok so if i don't have a correspondence with commune field i've got an error message so i need to change my script with that no?

arcpy.env.workspace = ready
for i in fcs:
    arcpy.MakeFeatureLayer_management(i, i + "_lyr")
    print('lyr')
    # fichier temporaire d'affinage de la selection
    for fc in fcs2:
        flds = [f.name for f in arcpy.ListFields(fc)]
        if 'nomcom' in flds:
            # build the query using field deliminators
            whereclause = f"""{arcpy.AddFieldDelimiters(fc, 'nomcom')} = 'SALEILLES'"""
            arcpy.MakeFeatureLayer_management(fc, fc + '_lyr', whereclause)
            # continue with copying
        else:
            print(f'commune not found in {fc}')
    if fc is not None:
        arcpy.SelectLayerByAttribute_management(i + "_lyr", 'NEW_SELECTION', fc)
        fc_desc = arcpy.Describe(i)
        print("fc_desc")
        print (fc_desc)
        out_name = fc_desc.baseName + '_export'
        print (out_name)
        new_name = i.replace('.','_')
        print(new_name)
        new_name2 = new_name.replace('rezo_h','')
        print(new_name2)
        ditched_features_path = os.path.join(out_name, sauve + "\_"+ new_name2)
        print (ditched_features_path)
        # copie de la selection en shape
        arcpy.env.overwriteOutput = True
        arcpy.CopyFeatures_management(i + "_lyr", ditched_features_path)
    else:
        print ("no data")

but have got always this error message

 

arcgisscripting.ExecuteError: ERROR 000358: Invalid expression
0 Kudos
by Anonymous User
Not applicable

If your fields change between featureclasses, and the sql needs to change between each featureclass, you can use if/elif to create your sql.

arcpy.env.overwriteOutput = True #<- move to top of script since it only needs to be set once


for fc in fcs2:
    # fichier temporaire d'affinage de la selection
    flds = [f.name for f in arcpy.ListFields(fc)]
    # create empty whereclause variable
    whereclause = None

    if 'commune' in flds:
        whereclause = f"""{arcpy.AddFieldDelimiters(fc, 'commune')} = 'Perpignan'"""

    elif 'nomcom' in flds:
        # build the query using field deliminators
        whereclause = f"""{arcpy.AddFieldDelimiters(fc, 'nomcom')} = 'SALEILLES'"""

    else:
        print(f'commune not found in {fc}')

    if whereclause:
        # describe first to get the name
        fc_desc = arcpy.Describe(fc)
        print("fc_desc")
        print (fc_desc)
        # create the temp layer with selection
        tmpLyr = arcpy.MakeFeatureLayer_management(fc, fc_desc.name + '_lyr', whereclause)
        out_name = fc_desc.baseName + '_export'
        print (out_name)
        new_name = fc_desc.name.replace('.','_').replace('rezo_h','')
        print(new_name)
        ditched_features_path = os.path.join(out_name, sauve + "\_"+ new_name)
        print (ditched_features_path)
        # copie de la selection en shape
        arcpy.CopyFeatures_management(tmpLyr, ditched_features_path)
    else:
        print ("no data")

 

0 Kudos
danielROUFFART1
New Contributor II

thanks for your help!

0 Kudos