I'm new to python and trying to write a python script using the field calculator in ArcMap for a specific field in an attribute table that will replace some of the values in the field but leave the other values as they are within the table. For example: ""fourth" must be changed to "4th" whereas "Neilston" should remain as it is in the field. The field in the table looks like this:
The python script used in the codeblock looks like this:
def streetNUM(field):
if field == "Fourth":
field.replace ("Fourth","4th")
elif field == "Fifth":
field.replace ("Fifth","5th")
else:
return field
And then in the box below the codeblock I have:
streetNUM(!FULLNAME!)
Nothing seems to change and I get an error code saying "Field is not nullable [FULLNAME]". Any suggestions as to where I may be going wrong with my code?
Solved! Go to Solution.
Thank you for the dictionary solution to this common issue, Xander Bakker. I have a similar question for parsing road names along with their suffixes and translating them to MSAG values. Interstates, County Roads, and State Roads follow a different pattern to our normal street names:
def translateNAME(streetName, dirSuf):
dctSTREETNAME = {"Interstate ": "I", "US Highway": "", " US Highway": "", "Martin Luther King Jr": "Dr King", "Saint": "St", "County Road ": "", "State Road ": ""}
for findSTREETNAME, replaceMSAGNAME in dctSTREETNAME.items():
if 'Interstate' in streetName and 'N' in dirSuf:
streetName = streetName.replace(findSTREETNAME, replaceMSAGNAME) + ' Northbound'
elif 'Interstate' in streetName and 'S' in dirSuf:
streetName = streetName.replace(findSTREETNAME, replaceMSAGNAME) + ' Southbound'
elif 'Interstate' in streetName and 'E' in dirSuf:
streetName = streetName.replace(findSTREETNAME, replaceMSAGNAME) + ' Eastbound'
elif 'Interstate' in streetName and 'W' in dirSuf:
streetName = streetName.replace(findSTREETNAME, replaceMSAGNAME) + ' Westbound'
elif 'County Road' in streetName or 'State Road' in streetName:
streetName = streetName.replace(findSTREETNAME, replaceMSAGNAME) + ' Hw'
else:
streetName = streetName.replace(findSTREETNAME, replaceMSAGNAME)
return streetName.strip()
When I run this script the normal streets are correct, but I get repeating concatenations for the others.
Any thoughts on how to fix this?
Hi jesterjace ,
You have your replace statements inside the loop, so it gets applied multiple times. I can't reproduce the same result as you have with many "Hw" in the resulting name, but maybe you should try something like this:
def translateNAME(streetName, dirSuf):
dctSTREETNAME = {"Interstate ": "I",
"US Highway": "",
"Martin Luther King Jr": "Dr King",
"Saint": "St",
"County Road ": "",
"State Road ": ""}
result = streetName
for findSTREETNAME, replaceMSAGNAME in dctSTREETNAME.items():
result = result.replace(findSTREETNAME, replaceMSAGNAME)
if 'Interstate' in streetName and 'N' in dirSuf:
result += ' Northbound'
elif 'Interstate' in streetName and 'S' in dirSuf:
result += ' Southbound'
elif 'Interstate' in streetName and 'E' in dirSuf:
result += ' Eastbound'
elif 'Interstate' in streetName and 'W' in dirSuf:
result += ' Westbound'
elif 'County Road' in streetName or 'State Road' in streetName:
result += ' Hw'
return result.strip()
I haven't tested the code though...
Perfection! Thanks, Xander Bakker!