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.

I am having trouble with using Python together with my gp.Calculatefield tool in my python script, and I am not sure why.. Heres the code (UPDATED)

import arcgisscripting, os, sys
myTempWorkspace = r"D:\ScheduledTasks\Temp\TempWorkspace_HyMS_Licences.gdb"
#workspace set up
gp = arcgisscripting.create()
gp.toolbox = "management"
gp.Workspace  = myTempWorkspace
gp.overwriteoutput = 1

codeblock = """\
def calField(Location):
    fldLocation = !LICENCE_2_LICENCE_0_LICENCE_LOCATION!

    if fldLocation <> " ":
        return !LICENCE_2_ROAD_NAME! + " (" + !LICENCE_2_LICENCE_0_LICENCE_LOCATION! + ")"
    else:
        return !LICENCE_2_ROAD_NAME!
"""

try:
    #gp.CalculateField_management(myTempWorkspace + os.sep + "LICENCE_3", "LOCATION", "[DWWEB_ADDRESS1] & \" (\" & [DWWEB_LOCATION] &\")\"","VB","")
    gp.CalculateField_management(myTempWorkspace + os.sep + "LICENCE_3", "LOCATION", "calField(Location)","PYTHON","codeblock")

    print "Calculate Field - Success"
except:
    print "Calculate Field - Failure"

I am getting the following error in the pythonWin IDE

>>> Unhandled exception while debugging...
Traceback (most recent call last):
  File "D:\temp\CalcFieldTest.py", line 21, in ?
    gp.CalculateField_management(myTempWorkspace + os.sep + "LICENCE_3", "LOCATION", "Location(newField)","PYTHON","codeblock")
RuntimeError: 
exceptions.NameError: name 'Location' is not defined
Failed to execute (CalculateField).
>>> Calculate Field - Failure

Now I am not sure why I am getting the error, I think I have done everything I should within the codeblock and on the gp.calculatefield line.

I'm using ArcGIS 9.3.1 So any help is appreciated.

share|improve this question
    
Have you tried running the Calculate Field tool from its dialog with the same code block? If not, then I think it is best to get that bit to work before trying to get it also working from Python. –  PolyGeo May 21 '12 at 10:57
    
I did try that, on a machine that had Desktop 10 on, but it was saying I had a syntax error on Line 2 of the codeblock. –  Halil Siddique May 21 '12 at 11:11
add comment

3 Answers 3

up vote 3 down vote accepted

To aggregate a bunch of the previous anwers/comments together, and add a few new changes, try this:

import arcgisscripting, os, sys 
myTempWorkspace = r"D:\ScheduledTasks\Temp\TempWorkspace_HyMS_Licences.gdb" 
#workspace set up 
gp = arcgisscripting.create() 
gp.toolbox = "management" 
gp.Workspace  = myTempWorkspace 
gp.overwriteoutput = 1 

codeblock = """def calField(fldLocation, fldName): 
    if fldLocation <> ' ': 
        return fldName + ' (' + fldLocation + ')' 
    else: 
        return fldName""" 

try:
    #gp.CalculateField_management(myTempWorkspace + os.sep + "LICENCE_3", "LOCATION", "[DWWEB_ADDRESS1] & \" (\" & [DWWEB_LOCATION] &\")\"","VB","") 
    gp.CalculateField_management(myTempWorkspace + os.sep + "LICENCE_3", "LOCATION", "calField(!LICENCE_2_LICENCE_0_LICENCE_LOCATION!, !LICENCE_2_ROAD_NAME!)","PYTHON",codeblock) 
    print "Calculate Field - Success" 
except: 
    print "Calculate Field - Failure" 
share|improve this answer
    
thanks a lot, its working perfectly, I think I need to do a bit more python. Thanks to everyone else for your help aswell!!! –  Halil Siddique May 22 '12 at 8:34
add comment

You need a colon at the end of your def statement.

def Location("newField"):

Edit (additional comments): You have a function called 'Location' and you appear to have a field also called 'Location'. Is that correct? You appear to be passing in the field !Location! as a variable to the function, but instead of a variable, you have a string "newField" (the quotes make this an string attribute and not a named variable), but you are not using this string. Later on you create a variable called newField (no quotes and therefore not a string). I'm not 100% clear what you are trying to do but I suspect you need to remove the quotes from around "newField" in the Def statement. if you are creating a new field, you'll also need an AddField statement somewhere.

share|improve this answer
    
I've added the colon, and when I try to run it i get the following exceptions.NameError: name 'Location' is not defined Failed to execute (CalculateField). –  Halil Siddique May 21 '12 at 12:58
    
Please see my edits above –  MappaGnosis May 21 '12 at 13:33
2  
Doesn't the function also need to return something? –  Chad Cooper May 21 '12 at 13:49
    
As Chad noted, replace newField = in your if and else statements with return. –  nmpeterson May 21 '12 at 14:07
1  
Remove the quotation marks on "codeblock" in your call to gp.CalculateField_management: with the quotes, you're just calling the string literal "codeblock", instead of the string defined by the variable codeblock :) –  nmpeterson May 21 '12 at 14:38
show 4 more comments

In your call to CalculateField:

gp.CalculateField_management(myTempWorkspace + os.sep + "LICENCE_3", "LOCATION", "calField(Location)","PYTHON","codeblock")

I think you need bangs around your input field in your function call:

gp.CalculateField_management(myTempWorkspace + os.sep + "LICENCE_3", "LOCATION", "calField(!Location!)","PYTHON","codeblock")

Also, it looks like you don't ever use your input argument anywhere in the function.

share|improve this answer
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.