In additional to what already has been provided above, you could use a dictionary to avoid longer if else statements. For instance:
def streetNUM(field):
dct = {"First": "1st", "Second": "2nd", "Third": "3rd",
"Fourth": "4th", "Fifth": "5th", "Sixth": "6th",
"Seventh": "7th", "Eighth": "8th", "Ninth": "9th"}
for find_txt, replace_txt in dct.items():
if find_txt in field:
field = field.replace(find_txt, replace_txt)
return field
In this case you will have a dictionary with all the values you may want to look for and the corresponding replace values. This is case sensitive. If you try this:
field = "First second Third NINTH Street"
print field
print streetNUM(field)
It will return this:
First second Third NINTH Street
1st second 3rd NINTH Street
This is because "second" and "NINTH" are not in the list.
To make it case insensitive you could use a simple regular expression:
def streetNUM_case_insensitive(field):
import re
dct = {"First": "1st", "Second": "2nd", "Third": "3rd",
"Fourth": "4th", "Fifth": "5th", "Sixth": "6th",
"Seventh": "7th", "Eighth": "8th", "Ninth": "9th"}
for find_txt, replace_txt in dct.items():
insensitive = re.compile(re.escape(find_txt), re.IGNORECASE)
field = insensitive.sub(replace_txt, field)
return field
Now when you run this code:
field = "First second Third NINTH Street"
print field
print streetNUM_case_insensitive(field)
... it will yield:
First second Third NINTH Street
1st 2nd 3rd 9th Street