3

I'm trying to apply the following code to extract features that contain these letters 'MW' within the field called 'CODE' for multiple layers. But I'm getting a SyntaxError for line 5 (defintion query).

import arcpy  
mxd = arcpy.mapping.MapDocument("Current")  
layers = arcpy.mapping.ListLayers(mxd)  
for layer in layers:  
    if layer.supports(definitionQuery)  
         layer.definitionQuery = ""CODE" LIKE "('%MW%')"
3
  • Could you please edit your question to include the formatted syntax error?
    – Aaron
    Commented Jun 11, 2019 at 2:19
  • Are you trying to extract the features, or update the map?
    – SMiller
    Commented Jun 11, 2019 at 2:37
  • Try removing the parentheses. e.g. """CODE LIKE '%MW%'""". You may also have to add quotes around CODE depending on the field and data type. I'm not sure if arcpy.AddFieldDelimiters works on layers.
    – SMiller
    Commented Jun 11, 2019 at 2:42

1 Answer 1

5

You need a colon after your line:

if layer.supports(definitionQuery)

should be:

if layer.supports(definitionQuery):

You should also consider modifying your query as was commented by @smiller. It will make your code easier to read.

2
  • Thanks, both the colon and the parentheses of my definition query solved the problem!
    – ArVP
    Commented Jun 11, 2019 at 3:20
  • If this answer helped you, please accept the answer so others know you found it useful.
    – Fezter
    Commented Jun 11, 2019 at 3:21

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.