arcpy.CalculateField_management) is not passing variable

2564
3
Jump to solution
03-08-2013 03:51 PM
ClaudineSicker
New Contributor III

Running as python script within toolbox in ArcGIS 10.1.  Trying to say that if file is empty, populate the fields "TypeName","PERCENTAGE", "Region" with the following values: nameVariable, 0, "Georgia", otherwise, populate the table fields "Region" with "Georgia" and the field "TypeName" with "Goldfish". I cannot seem to populate the TypeName field with a variable. What am I doing wrong. I have spent two days on this and I cannot figure it out!

# Geonet conversion put code in a single line.  Below is another user's attempt to get python indentation
# fixed to make sense of the "answer".  Should be good thru line 20....not sure after that.

import os.path 
import arcpy 
import arcgisscripting  
myList = ("Fish", "Reptile","Aracnid", "Other") 
geoLoc = "C:\\Data\\TempData.gdb\\"  
for variablefile in myList:
  #  Assigning variables      
  species = geoLoc + (variablefile)+ "_Buffer"  
  # setting nameVariable     
  if variablefile == "Fish":
    nameVariable = "Goldfish"
  elif variablefile == "Reptile":
    nameVariable = "Tree frog"
  elif variablefile == "Aracnid":
    nameVariable = "Black widow"
  else:
    nameVariable = "Household pet"
  addRow =arcpy.da.InsertCursor(species,("TypeName","PERCENTAGE", "Region"))
  result = int(arcpy.GetCount_management(species).getOutput(0))
  if result == 0:
    addRow.insertRow((nameVariable, 0, "Georgia"))
  else:
    arcpy.CalculateField_management(species, "Region", "\"Georgia\"", "PYTHON", "")
    arcpy.CalculateField_management(species, "TypeName", 'nameVariable', "PYTHON", "")
    #arcpy.CalculateField_management(species, "Name", "\"Goldfish\"", "PYTHON", "")  Works but doesn't use variable name     
exit  



I get the following error message: 

ExecuteError: ERROR 000539: Error running expression: commonName 
Traceback (most recent call last): 
  File "<expression>", line 1, in <module> 
NameError: name 'commonName' is not defined 


Pretty sure it is failing at nameVariable (bold). 

Thanks in advance.

Question from Mar 8, 2013 6:51 PM Message was edited by: Timothy Hales Formatted Code Block

Message was edited by: Timothy Hales

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
curtvprice
MVP Esteemed Contributor
Woohoo!


I highly recommend using nested quotes and substitution - easier to read and debug. This is equivalent to your solution:

arcpy.CalculateField_management(species, "TypeName", \     '"{0}"'.format(nameVariable), "PYTHON")

View solution in original post

3 Replies
ClaudineSicker
New Contributor III
Oops. Sorry. Copied wrong message. (That happens when you spend days on a single line of code. You get punchy and careless.) The actual message is:

ExecuteError: ERROR 000539: Error running expression: nameVariable
Traceback (most recent call last):
File "<expression>", line 1, in <module>
NameError: name 'nameVariable' is not defined

Happens on last executable line before "Exit"

Still stuck on this. Can't figure out how to get it to do what I want. Hopefully with the correct error message, someone can figure this out for me. I don't know where to go from here.
0 Kudos
ClaudineSicker
New Contributor III
Had a eureka moment and figured it out. Correct format is:

arcpy.CalculateField_management(species, "TypeName", "\"nameVariable\"", "PYTHON", "")


Woohoo!
0 Kudos
curtvprice
MVP Esteemed Contributor
Woohoo!


I highly recommend using nested quotes and substitution - easier to read and debug. This is equivalent to your solution:

arcpy.CalculateField_management(species, "TypeName", \     '"{0}"'.format(nameVariable), "PYTHON")