I am getting a syntax error in my script but I know from testing it in ArcMap attribute table, the syntax is fine. Anyone have any idea why this code doesn't work? I see that a search cursor has been used in this situation but (a) I'm not familiar with them and (b) why wont the way I'm doing it work? Thanks for any help, time and consideration.
EDIT* - I have added an alternate working codeblock to the script below. Seems both ways work just fine!
import arcpy, os
from arcpy import env
#Path of the workspace
in_workspace = r"C:\folder"
fieldName = "Symbology"
expression = "Class(!City!)"
This is also another way that it works. So both work just fine. Thanks again for all the help!
code ='''
def Class(City):
if 'Milwaukee' in City:
return 2
elif 'Kenosha' in City:
return 4
elif 'Madison' in City:
return 9
else:
return 0
'''
for dirpath, dirnames, filenames in arcpy.da.Walk(in_workspace, datatype="FeatureClass",type="All"):
if "skipFolder" in dirnames:
dirnames.remove('skipFolder')
for filename in filenames:
if "City" in [f.name for f in arcpy.ListFields(os.path.join(dirpath, filename))]:
arcpy.AddField_management(os.path.join(dirpath,filename), fieldName, "SHORT", "", "", "", "", "NULLABLE")
arcpy.CalculateField_management(os.path.join(dirpath,filename), "Symbology", expression, "PYTHON", code)
else:
print "no City field for dataset ", filename
#Prints text that shows script has finished running
print "message that reflected field calc use"
print "Finished script"
"Symbology"
? Or is it rather the field"City"
? – ustroetz Nov 14 '13 at 18:33