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 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"
share|improve this question
    
Are you sure you want to use the field calculator in the field "Symbology"? Or is it rather the field "City"? –  ustroetz Nov 14 '13 at 18:33
    
Yes, I want to use it in "Symbology". The idea is that I can control how to symbolize with a numeric value instead of using the actual city field while maintaining the "City" field values. Thanks –  cbrannin Nov 14 '13 at 18:51

1 Answer 1

up vote 1 down vote accepted

Try to substitute the code variable with this:

code ='''
def Class(City):
    if City == 'Milwaukee':
        return 1
    elif City == 'Kenosha':
        return 2
    else:
        return 5
'''
share|improve this answer
    
This worked! My only question is why? Any insight on why it works this way and not the other? Very happy though thank you very much. Cheers! –  cbrannin Nov 14 '13 at 18:47
    
Glad that it worked. It's only a way to keep more readable and clean the code. However there was an elif missing, so all ones became fives. –  afalciano Nov 14 '13 at 18:54
    
So strange that it worked with no issues on a merged version of the dataset with no issues (using the original codeblock) inside of the attribute table. Funny I tried City = 'Milwaukee' with no luck, guess I have much to learn! Thanks again for the help. –  cbrannin Nov 14 '13 at 18:58
    
@afalciano & ustroetz I fixed the above code with an alternate example that works just as well. Thanks again. –  cbrannin Nov 14 '13 at 19:18
1  
@ustroetz in works for string variables too: 'Kenosha' in 'Where is Kenosha?' is True –  Mike T Nov 14 '13 at 19:55

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.