Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

just wondering if anyone else is having issues with the field calculator in ArcGIS 10 SP3. In the following code I do a pretty simple field calculation with a variable (an array) as one of the parameters in the expression.

exp1 = "Feature = 16"
rows = arcpy.SearchCursor("ObservationMaster", exp1)
li = []
for row in rows:
    ObsID = int(row.getValue("ObsID"))
    li.insert(len(li), ObsID)
if li != []:
    codeblock = """def chStat(stat, ID, li):
    if int(ID) in li:
        return 5
    else:
        return int(stat)"""

exp = """chStat(str(!ManagementStatus!), str(!ObsID!), li)"""

arcpy.CalculateField_management("ObservationDetail", "ManagementStatus", exp, "PYTHON", codeblock)

When I run it through IDLE (Python 2.6.5), there are no problems and it runs successfully. However, if I create a script in a toolbox in ArcCatalog/ArcMap using this same code, I get the following error:

<class 'arcgisscripting.ExecuteError'>: ERROR 000539: Error running expression: chStat(str(1), str(1), li) <type 'exceptions.NameError'>: name 'li' is not defined
Failed to execute (CalculateField).

I know this is because it is not treating "li" as a variable, but it does when I run it in IDLE. Has anyone had this same problem and does anyone know a solution to get this working. I really need this script to run via ArcMap.

Thanks in advance

share|improve this question
    
Correct me if I'm working but it looks like your exp and codeblockvariables are commented out with the multi-line comment –  Roy Jul 17 '12 at 11:23
    
@Roy, Yes the codeblock is commented out with multi-line comment because that's the only way I could get it to all stay commented out. And the exp is also multi-line commented out, but changing it to single line comment has no effect, I still get the same error. However, do you think maybe the multi-line comment in the exp is the problem? If so, is there a different way to do it, that would work? Thanks –  Aubrey Jul 17 '12 at 14:42
    
Maybe I'm not understanding how your script works -- it looks to me like you're calling variables that are empty because they've been commented out –  Roy Jul 17 '12 at 15:54
    
@Roy, yes I am calling variables full of comments. They are being used as the codeblock and expression part of the arcpy Field Calculator. They have to be commented out like that in order to be treated as strings and read into the Field Calculator. –  Aubrey Jul 17 '12 at 16:09
add comment

1 Answer

up vote 0 down vote accepted

Found a solution and just thought I would answer my own question, just incase other people come across this same issue and thread.

To solve it, I simply moved all the code inside the "codeblock" as follows:

codeblock = """exp1 = "Feature = 16"
rows = arcpy.SearchCursor("ObservationMaster", exp1)
li = []
for row in rows:
    ObsID = int(row.getValue("ObsID"))
    li.insert(len(li), ObsID)

def chStat(stat, ID, li):
    if int(ID) in li:
        return 5
    else:
        return int(stat)"""

exp = """chStat(str(!ManagementStatus!), str(!ObsID!), li)"""

arcpy.CalculateField_management("ObservationDetail", "ManagementStatus", exp, "PYTHON", codeblock)

The code now works successfully in IDLE and as a script in ArcMap. The first part of the codeblock (before the def) only runs once (though it looks like it would run on every row that the field calculator is used on), which makes the script efficient as well.

share|improve this answer
    
@Aubrey- since you found your own solution, you can go ahead and mark your answer as "accepted" to close out the question. –  RyanDalton Aug 31 '12 at 18:09
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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