I am trying to write a field calculation script that changes road names with abbreviations to the full name. Example: Day Dr. => DAY DRIVE or Phillips Rd. => PHILLIPS ROAD. I wrote and tested the function in Jupyter notebook and everything works fine. I got the script to work on my points feature class but the same script will not work on the same field in my line feature class. Any suggestions?
def street_name_fix(StreetName):
StreetName = StreetName.rstrip('.')
if StreetName is not None:
if StreetName != '':
if StreetName.split()[-1].upper() == 'RD':
return '{} {}'.format(' '.join(StreetName.split()[:-1]), 'ROAD')
elif StreetName.split()[-1].upper() == 'CIR':
return '{} {}'.format(' '.join(StreetName.split()[:-1]), 'CIRCLE').upper()
elif StreetName.split()[-1].upper() == 'DR':
return '{} {}'.format(' '.join(StreetName.split()[:-1]), 'DRIVE').upper()
elif StreetName.split()[-1].upper() == 'LN':
return '{} {}'.format(' '.join(StreetName.split()[:-1]), 'LANE').upper()
elif StreetName.split()[-1].upper() == 'CT':
return '{} {}'.format(' '.join(StreetName.split()[:-1]), 'COURT').upper()
elif StreetName.split()[-1].upper() == 'PL':
return '{} {}'.format(' '.join(StreetName.split()[:-1]), 'PLACE').upper()
elif StreetName.split()[-1].upper() == 'ST':
return '{} {}'.format(' '.join(StreetName.split()[:-1]), 'STREET').upper()
elif StreetName.split()[-1].upper() == 'BLVD':
return '{} {}'.format(' '.join(StreetName.split()[:-1]), 'BOULEVARD').upper()
else:
return StreetName.upper()
else:
return ''
else:
return StreetName
Solved! Go to Solution.
My solution would look something like this:
def street_name_fix(StreetName):
replacements = {'RD': 'ROAD',
'CIR': 'CIRCLE',
'DR': 'DRIVE',
'LN': 'LANE',
'CT': 'COURT',
'PL': 'PLACE',
'ST': 'STREET',
'BLVD': 'BOULEVARD'}
StreetName = StreetName.upper().strip().rstrip('.')
try:
return '{} {}'.format(' '.join(StreetName.split()[:-1]), replacements[StreetName.split()[-1]])
except IndexError:
return StreetName
except KeyError:
return StreetName
It is failing because you are .split-ing a string that contains a single whitespace in to a list - the result of this is an empty list.
>>> a = u" "
>>> a.split()
[]
Thanks Pete Crosier! I guess check for the space string first? Any suggestions on how to do this for 1 or multiple spaces?
Cheers,
Damien
My solution would look something like this:
def street_name_fix(StreetName):
replacements = {'RD': 'ROAD',
'CIR': 'CIRCLE',
'DR': 'DRIVE',
'LN': 'LANE',
'CT': 'COURT',
'PL': 'PLACE',
'ST': 'STREET',
'BLVD': 'BOULEVARD'}
StreetName = StreetName.upper().strip().rstrip('.')
try:
return '{} {}'.format(' '.join(StreetName.split()[:-1]), replacements[StreetName.split()[-1]])
except IndexError:
return StreetName
except KeyError:
return StreetName
Pete Crosier thank you so much! Much more Pythonic and simple!!! Much appreciated! This works beautifully!
What is the business need to using the full name? Are you keeping the abbrevation and just adding the full name to a new field? I'm asking because ESRI's local government information model (LGIM0 which is used as the schema for out-of-the-box (OOTB) solutions uses abbreviations so you would want to keep the original field information if you ever plan to use the OOTB solutions. At the least just something to think about.
This is how the client wants it in the end...
throw this line at the top of your scripts
# -*- coding: utf-8 -*-
it is a caution for Unicode encoding vs other forms, especially if you are using python 2.7 and your data may have been developed in a post 2.7 world
I realized when I converted to ansi in Notepad++ that it would run. Maybe this is related.