0

I'm using this code to remove all decimal places within the symbology results of Graduated Colors. I can get it to apply to one feature class, but I can't figure out how to get it to apply to all of them (I have a total of 15).

Here is the code I'm using:

p = arcpy.mp.ArcGISProject("CURRENT")
m = p.listMaps()[0]
lyr = m.listLayers("BCBS*")[0]
sym = lyr.symbology
if sym.renderer == "GraduatedColorsRenderer":
    breaks = sym.renderer.classBreaks
    for b in breaks:
        b_int = b.label[0:-7]
        b.label = b_int
lyr.symbology= sym

All the feature classes begin with "BCBS" so I thought that BCBS* would apply the desired results to all the feature classes but it didn't. Then I realized that [0] was only applying it to the first feature class, but I still can't figure out how to apply it to all feature classes that begin with "BCBS"

3

What you want to do is called a "Loop" in python - you want to loop through all the layers. Currently you are specifying only one layer. Instead of lyr = m.listLayers("BCBS*")[0] get a list of all your layers, such as layers = m.listLayers("BCBS*"), and then loop through that list of layers using for lyr in layers:

Note that those two lines are the only things I have changed in your code (as well as indenting the lines in the for), so it assumes that you want the symbology set the same way for every layer in the list.

p = arcpy.mp.ArcGISProject("CURRENT")
m = p.listMaps()[0]
layers = m.listLayers("BCBS*")
for lyr in layers:
    sym = lyr.symbology
    if sym.renderer == "GraduatedColorsRenderer":
        breaks = sym.renderer.classBreaks
        for b in breaks:
            b_int = b.label[0:-7]
            b.label = b_int
    lyr.symbology = sym
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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