I am trying to replace the values in my predirection field in ArcMap using the field calculator. Here is my sample code.
def predir(field):
if "S" in fields:
return field.replace("S","South")
elif "E" in fields:
return field.replace("E","EAST")
elif "W" in fields:
return field.replace("W", "WEST")
elif "N" in fields:
return field.replace("N", "NORTH")
elif "NE" in fields:
return field.replace("NE", "NORTHEAST")
elif "NW" in fields:
return field.replace("NW", "NORTHWEST")
elif "SE" in fields:
return field.replace("SE", "SOUTHEAST")
elif "SW" in fields:
return field.replace("SW", "SOUTHWEST")
else:
return field
I am getting the following error:
Failed to execute. Parameters are not valid
Error 000989: Python syntax error: Parsing error SyntaxError:Invalid syntax(line18)
Failed to execute (Calculate Field).
There are Null values in the field, but they need to be there for NextGen 911 purposes. Any suggestions? I was thinking of using a similar syntax for replacing my post direction and street type values as well. Eventually I will need to put a script together that does all this for my street center lines and structures.
Solved! Go to Solution.
Try this:
def predir(field):
name_mapping = {
"S": "SOUTH",
"SW": "SOUTHWEST",
"W": "WEST",
"NW": "NORTHWEST",
"N": "NORTH",
"NE": "NORTHEAST",
"E": "EAST",
"SE": "SOUTHEAST"
}
if field in name_mapping:
return name_mapping[field]
else:
return field
Tyquin - I'll be the first to say I'm am NOT a Python programmer by any stretch of the imagination but in researching your question a little bit, I learned that Python considers the NULL value to be NONE in Python. Continuing I believe based on my limited research, you'll need to create a "search cursor" to ignore the NONE/NULL value in the field. If it finds one, it ignores it and goes to the next roll/column. If there are no NULL/NONE in the field then it executes your script portion above.
I'm tagging Jeff Bigos's Blog as he is Esri Training Services "python guru" to see if he has a more detailed answer than I but it seems to make sense...
Try this:
def predir(field):
name_mapping = {
"S": "SOUTH",
"SW": "SOUTHWEST",
"W": "WEST",
"NW": "NORTHWEST",
"N": "NORTH",
"NE": "NORTHEAST",
"E": "EAST",
"SE": "SOUTHEAST"
}
if field in name_mapping:
return name_mapping[field]
else:
return field
The code worked. I will use the same syntax for the road types and post directional. Thank you very much. If you have any alternative solutions that achieve the same result, please let me know. I am always looking for ways to improve my python code.