I am relatively new to python in ArcGIS.

I have a shapefile loaded in ArcGis (v 10.0) and will select two adjacent polygons using the selection tool. Once I select my two polygons, I want to use Python to examine the underlying records for those selected polygons, pull out some data from certain fields in the underlying attribute table, do some simple math using those data, and then write that result back out to the same attribute table (in an existing field - i.e. I will overwrite data in that field).

Here's the code I have so far:

import arcpy
#this is the name of the shapefile that's loaded in ArcMap
lyr_nm = "layer name"

#Get the current map layers
mxd = arcpy.mapping.MapDocument("CURRENT")
lyr = arcpy.mapping.ListLayers(mxd, lyr_nm)[0] 
#this gets the layer but I need a feature class to examine the underlying records

#set field variable names
acresField = "ACRES"
tcovField = "TreeCover"
sCovField = "ShrubCover"
sourceField = "Source"


    #Check for exactly 2 selected features.
    if len(desc.FIDSet) != 2:
        arcpy.AddMessage("Please select only two polygons to merge!")

    napaAc = 0
    knoxAc = 0
    totalAc = 0
    napaTree = 0
    napaShrub = 0
    knoxTree = 0
    knoxShrub = 0
    treePct = 0
    shrubPct = 0

    #somewhere here I need to define a feature class based on the layer above,
    #and the feature class must only have those two
    #selected records in its result, otherwise don't this will work

    rows = arcpy.SearchCursor(fc)

    for row in rows:
        if row.getValue(sourceField) == 0:
            napaAc = row.getValue(acresField)
            totalAc = totalAc + napaAc
            napaTree = row.getValue(tcovField)
            napaShrub = row.getValue(scovField)
            knoxAc = row.getValue(acresField)
            totalAc = totalAc + knoxAc
            knoxTree = row.getValue(tcovField)
            knoxShrub = row.getValue(scovField)

    #calculate values to write back out to Knoxville record
    shrubPct = ((napaAc/totalAc)*napaShrub) + ((knoxAc/totalAc)*knoxShrub)
    treePct = ((napaAc/totalAc)*napaTree) + ((knoxAc/totalAc)*knoxTree)

    rows = arcpy.UpdateCursor(fc)

    for row in rows:
        if row.getValue(sourceField)==1:


     # Report an error messages
     arcpy.AddError("Could not complete the polygon join!")

     # Report any error messages  

closed as off-topic by PolyGeo Mar 5 '16 at 12:07

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions seeking help to debug/write/improve code must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Providing a clear problem statement and evidence of a code attempt will help others to help you. See: How to create a Minimal, Complete, and Verifiable example." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    What have you already tried? Are you having a specific issue with your code? Please paste some code snippets showing where you are stuck, so we have some additional information. gis.stackexchange.com/help/how-to-ask – MaryBeth Mar 4 '16 at 14:19
  • 2
    This is certainly possible. It's even simple to accomplish. But coding questions here in GIS SE are expected to include code, so you'll need to crack open the documentation and take your best shot at getting something working. Then, if you have issues, you can update this question with the exact code that is giving you fits. – Vince Mar 4 '16 at 14:20
  • Would you be able to edit your question to present just a code snippet that illustrates precisely where you are stuck, please? There's no hard and fast rule about how big a code snippet can be but I think anything more than about 10 lines of code is usually getting beyond a code snippet. At one point you are initializing 9 variables - at a quick guess why not just initialize and use one while testing. – PolyGeo Mar 5 '16 at 12:06