I have a function that standardizes addresses in a table based on values in a dictionary. The function when used in a script by itself works perfectly well and updates all records appropriately. However, when I call the function inside another module, it only changes records where it finds changes are needed. I solved the issue in the code posted below by adding an 'else' statement, but it is unclear to me why this should work differently in one context and not another.
def update_address():
# in_fc is a table of values defined outside the function
arcpy.management.AddField(in_fc, "AddressOriginal",'text', "","", 200, "Original Address")
arcpy.management.CalculateField(in_fc,'AddressOriginal',"!Address!")
# function standardizes addresses
def ModAddr(field):
dct = {" Northeast": " NE", " Northwest": " NW"," Southeast": " SE", " Southwest": " SW",", Albuquerque, NM": "", " BL ": " BLVD ",
" Avenue ": " AV "," Boulevard ": " BLVD ", " BY ": " BYPASS ",
" CI ": " CIR "," Circle ": " CIR ", " Court ": " CT ",
" Drive ": " DR ", " Freeway ": " FRWY ", " FY ": " FRWY "," Lane ": " LA ", " LP ": " LOOP ", " PY ": " PKWY ",
" Place ": " PL "," Road ": " RD ", " Street ": " ST ", " TL ": " TRL ", " Way ": " WY ", " AVE ": " AV ", " EX ": " EXT "}
for find_txt, replace_txt in dct.items():
if find_txt in field:
field = field.replace(find_txt, replace_txt)
else: # added this else statement and function works fine
field = field
return field
with arcpy.da.UpdateCursor(in_fc,"Address") as cursor:
for addr in cursor:
addr[0] = ModAddr(addr[0])
cursor.updateRow(addr)