I am trying to assign a value to two shapefiles in the first data frame of my .mxd with definition query, but it seems that cannot be done.
Any suggestion? Thanks
Here is the code:
import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
for lyr in arcpy.mapping.ListLayers(mxd):
if lyr.name == "waste":
lyr.definitionQuery == "Code = 'IT1300021'"
what is the error or what is the result?
lyr.definitionQuery == .... this is an equality check not an assignment, which is what I think you want
good note, but is not working either with that. I get an error Runtime error SyntaxError: can't assign to comparison (<string>, line 6)
So no error? just nothing is happening?
It might be time to add
if lyr.supports("DEFINITIONQUERY"):
to the test to ensure your layers support a query and a couple of print statement to monitor progress, otherwise a malformed query should have at least thrown an error.
I wrote that but...
import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
aa = "'IT1300021'"
for lyr in arcpy.mapping.ListLayers(mxd):
if lyr.supports("DEFINITIONQUERY"):
if lyr.name == "waste":
lyr.definitionQuery == "Code = 'IT1300021'"
I ve also tried this, but didn't work also
import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
aa = "'IT1300021'"
for lyr in arcpy.mapping.ListLayers(mxd):
if lyr.name == "waste":
lyr.definitionQuery == "'Code = {}'".format(aa)
In both of your follow-up code snippets, you are still using an equality check (==) instead of an assignment (=). Even if all of the lines up to the final line worked, the final line of code isn't' going to update anything, it is only going to return a True or False that has no impact on anything.
well lacking print statements, it isn't possible to see the code even gets into the sections where the definitions are being set. I can only refer you to the help topic where it states that the source and type of the layer is important, case sensitivity applies to one type and not another and that changes are not automatic but an update may need to be made. Hence, my recommendation about print statements to see what is going on....it is probably something obvious
http://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-mapping/updatingandfixingdatasources.htm
http://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-mapping/layer-class.htm
Like Dan said, you should use = instead of ==.
import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
for lyr in arcpy.mapping.ListLayers(mxd):
if lyr.name == "waste":
lyr.definitionQuery = "Code = 'IT1300021'"
arcpy.RefreshActiveView()
What's the status of this issue?