Hi Everyone,
I'm trying to do something fairly simple with Python (ArcGIS 10.3): populate a field called StreetNum with street addresses taken from a field called StreetAddr. However, some street addresses have a one-digit unit number surrounded by spaces before the unit number (e.g. 3 - 560 Main St) and I want only the street number to appear in the StreetNum field. So in other words, I want to populate StreetNum based on criteria: if there is a hyphen present in the string for a particular row in StreetAddr, then calculate the StreetNum by skipping the first 4 values of StreetAddr; otherwise, use the values of StreetAddr the way they are.
I've tested the cases and they work. However, I cannot come up with the proper criteria to check for a hyphen in a given row (no matter how I change my criteria, the script either passes or fails all rows). How would I correctly word my loop criteria?
# Insert Search Cursor into StreetAddr field
rows = arcpy.SearchCursor(fc,["StreetAddr"])
# Check for hyphens in this field
for row in rows:
if "-" in str(row):
#Calculate the field by removing the unit number and hyphen from StreetAddr
arcpy.CalculateField_management(in_table=fc, field="StreetNum", expression="Mid( [StreetAddr],5 )", expression_type="VB", code_block="")
else:
#Calculate the field by taking the content from StreetAddr
arcpy.CalculateField_management(in_table=fc, field="StreetNum", expression="[StreetAddr]", expression_type="VB", code_block="")
del rows
del row
Solved! Go to Solution.
In the last dialog box, where you have !StreetAddr!, you will need to call the function you created in the dialog box above. You should have the following:
calc(!StreetAddr!)
Hi Dianne,
You can accomplish this using the Field Calculator. Ex:
Pre-logic script code:
def calc(field1): if "-" in field1: return field1.split(" ", 2)[-1] else: return field1
StreetNum=
calc(!StreetAddr!)
Be sure 'Python' is checked at the top of the field calculator:
Hi Jake,
Can you please explain the syntax of split? What do the numbers 2 and -1 correspond to?
I tried using that section of code, but Field Calculator ended up calculating all records to match StreetAddr (in other words, nothing passed the test). Why would this be the case? There are two records that have unit numbers, and all I want is the street numbers without the unit number (e.g. 7 - 364 Lougheed Rd should become 364 Lougheed Rd) (I realize my wording above might have been a bit confusing). I know I could fix this manually but for acquiring more Python experience I'd like to know how to address this with Python. What else could you suggest?
if it were as simple as one case, why don't you examine the following conceptually
>>> x
'3 - 560 Main St'
>>> "- " in x
True
>>> y = x.split("- ")[1]
>>> y
'560 Main St'
Note, I am checking for "dash space" not just "dash"
If found (True), then split on the aforementioned and take the first fiddly bit (counting from 0... ergo the last piece of the split
The split method will split the string on a space (" "). The '2' in this syntax is telling the method to stop after the 2nd space is found. For example, take the following address:
7 - 364 Lougheed Rd
If I were to run the following:
text = '7 - 364 Lougheed Rd' print text.split(" ", 2)
The following list would be returned:
['7', '-', '364 Lougheed Rd']
The '-1' in the syntax is going to return the last value in the list. In this case, it will return '364 Lougheed Rd'.
If you want to upload a sample of the data, I could take a look at it.
So here are the addresses that get calculated into StreetNum directly from StreetAddr. No splitting was done for the hyphenated fields when I copied your above code as follows:
In the last dialog box, where you have !StreetAddr!, you will need to call the function you created in the dialog box above. You should have the following:
calc(!StreetAddr!)
Oh wow, it worked! That was easy! Thanks!
Have you thought about using the Standardize Addresses—Help | ArcGIS for Desktop tool and joining the table and calculating the desired results.
I second Wes's comment, especially since the tool is available at all licensing levels. There are also several Python-based packages that have similar functionality. I personally like usaddress, but I only work with US addresses.