Within a script I create a new file, which has a dynamic value in the attribute name. This also changes the name of the attribute [FID] in each file. During a query in a file I have to use this attribute. So I have to create the field name for the query expression. But this is where I fail because of the syntax of all the quotation marks and backslashes.

The "normal" (undynamic) query would look like this:

    "\"FID_4113_T\" <> -1 AND \"FID_4113_L\" <> -1"

Doc: https://desktop.arcgis.com/en/arcmap/10.3/tools/data-management-toolbox/select-layer-by-attribute.htm

My problem is that I cannot integrate the value of the cursor (row[0].zfill(4)) into the field name [FID_*row[0].zfill(4)*_T]

1 Answer 1


You can use the format() method to include specific values inside your strings. Furthermore, I think the field name does not have to be inside quotes but I may be wrong. You could write the following inside your for loop:

for row in cursor:
    fid = row[0].zfill(4)
    query = '''FID_{}_T <> -1 AND FID_{}_L <> - 1'''.format(fid, fid)
    arcpy.SelectLayerByAttribute_management("Union", "NEW_SELECTION", query)

If it does not work, give it a try by quoting the field names. No need to escape the quotes as the query has triple single quotes.

  • 1
    it works nearly perfect 8-) I had only to change the second line into fid = str(row[0]).zfill(4) Thanks a lot for your super fast help
    – Kunibert
    Commented May 8, 2020 at 21:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.