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
Solved! Go to Solution.
Woohoo!
arcpy.CalculateField_management(species, "TypeName", \ '"{0}"'.format(nameVariable), "PYTHON")arcpy.CalculateField_management(species, "TypeName", "\"nameVariable\"", "PYTHON", "")
Woohoo!
arcpy.CalculateField_management(species, "TypeName", \ '"{0}"'.format(nameVariable), "PYTHON")