I recently taught a class in Portland, OR where the students asked about changing a definition query in Python. Well, I thought this was simple -- and it is, but I didn't get there the easy way! I would assume that others may also have this challenge so I thought I would post this little challenge.
First, I
thought I would set a varible to an existing definition query (so I'd see exactly how one looks as it comes from ArcPy). I have a map document that contains a layer named "Survey Grid Boundary" with a definition query.
In the python window, I typed the following lines:
>>> mxd = arcpy.mapping.MapDocument("CURRENT")
>>> layers = arcpy.mapping.ListLayers(mxd,"Survey*")
>>> print layers[0].definitionQuery
"TOWNSHIP_C" = 'C7' AND "RANGE_CD" = '2'
Cool, I thought! This is easy! So then I thought I could just change the statement with a query held in a variable. I just wanted to change the last number from a 2 to a 3. So I entered (and I knew this would cause troubles):
>>> query = "TOWNSHIP_C" = 'C7' AND "RANGE_CD" = '3'
Parsing error SyntaxError: invalid syntax (line 1)
Of course we all know that python uses quotes (either " or ') to enclose strings. Dummy me immediately thought of the str() function. WRONG! Then I thought of just quoting everything in quotes. WRONG! Then I started searching for a function to quote complex strings. WRONG!!! I was getting my self into a rabbit hole...
So then a basic issue hit me... the issue about path names and using the backslash '\'. And we've learned that the backslash is an escape character. So I came up with:
>>> query = '\"TOWNSHIP_C\" = \'C7\' AND \"RANGE_CD\" = \'3\''
>>> layers[0].definitionQuery = query
>>> print layers[0].definitionQuery
"TOWNSHIP_C" = 'C7' AND "RANGE_CD" = '3'
It works perfectly! And, it's pretty darn simple.
So just remember the the backslash can be your friend and there are two single quotes at the end of the variable assignment line rather than a double quote.
Using Jeff's comment, I thought I would respond to his post and put the statements into courier font to read easier... well, it didn't work so I'm adding that here where I can control font. Thanks, Jeff!
query = '"TOWNSHIP_C" = \'C7\' AND "RANGE_CD" = \'3\''
or
query = """"TOWNSHIP_C" = 'C7' AND "RANGE_CD" = '4'"""
So, which is cleaner and easier to read??? It probably comes down to your choice. That's fun with Python!
Thank you, Jeff.