Select to view content in your preferred language

Remove predirectional from attribute table

1041
7
Jump to solution
04-12-2018 08:08 AM
CCWeedcontrol
Frequent Contributor

I have a table that i am trying to remove just the street predirectional's of the table. I have the following code but it removes more than just the predirectional's from the street name. For example N St Alphonusu Way to turn into T Alphonsus Way. I just want to remove the "N", "E", "W" & "S" from the street name. How can do this correctly?

lyr = "C:\Temp\Default.gdb\CurrntRoadNamesTableTest_1"


for field in arcpy.ListFields(lyr, "*", "String"):  
    with arcpy.da.UpdateCursor(lyr, ['STREET']) as cursor:  
        for row in cursor:  
          s = row[0]
          row[0] = s.lstrip('W ')
          a = row[0]     
          row[0] = a.lstrip('E ')
          b = row[0]
          row[0] = b.lstrip('N ')
          X = row[0]
          row[0] = X.lstrip('S ')

          cursor.updateRow(row)
del cursor
0 Kudos
1 Solution

Accepted Solutions
MitchHolley1
MVP Regular Contributor

This is a quick/dirty way to do it.  I'm sure there is a more elegant way. 

lyr = "C:\Temp\Default.gdb\CurrntRoadNamesTableTest_1"

preDir = ['N ','S ','E ','W ']

#for field in arcpy.ListFields(lyr, "*", "String"): <- i don't know why you need this 
with arcpy.da.UpdateCursor(lyr, ['STREET']) as cursor:  #since you're specifying field in cursor item
    for row in cursor:
        if row[0][:2] in preDir:
            row[0] = row[0][2:]
            cursor.updateRow(row)
del cursor‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

7 Replies
MitchHolley1
MVP Regular Contributor

This is a quick/dirty way to do it.  I'm sure there is a more elegant way. 

lyr = "C:\Temp\Default.gdb\CurrntRoadNamesTableTest_1"

preDir = ['N ','S ','E ','W ']

#for field in arcpy.ListFields(lyr, "*", "String"): <- i don't know why you need this 
with arcpy.da.UpdateCursor(lyr, ['STREET']) as cursor:  #since you're specifying field in cursor item
    for row in cursor:
        if row[0][:2] in preDir:
            row[0] = row[0][2:]
            cursor.updateRow(row)
del cursor‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
CCWeedcontrol
Frequent Contributor

can you explain what lines 8 & 9 are doing please?

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

In Python, strings are a sequence type (Sequence Types — str, unicode, list, tuple, bytearray, buffer, xrange), and sequences support slicing, which is what lines 8 & 9 is doing.  row[0] returns the first item in a list, which happens to be a string in this case, and [2:] slices the string. 

CCWeedcontrol
Frequent Contributor

So after a closer look it seems as this is remove a lot of street names what begin with 'N ','S ','E ','W ' not just the pre directionals. Any ideas?

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Can you give a few sample addresses, especially ones you are having issues with?

0 Kudos
CCWeedcontrol
Frequent Contributor

I need to remove just the pre-directionals and keep the name.

Thanks.

E El Jay Ct --> El Jay Ct

E Elgin St --> Elgin St

E Elk River St --> Elk River St

0 Kudos
CCWeedcontrol
Frequent Contributor

I have some what of a solution starting at line 9 - 12 but the problem is that i am not sure how to do more than just one at a time.

currently the following just does 'N ', i need it to do 'N ','S ','E ','W '. How can accomplish this?

'''
prefixes = ('N ','S ','E ','W ')
with arcpy.da.UpdateCursor(lyr, 'STREET') as cursor:
    for street, in cursor:
        if street and street.lstrip(prefixes):
            cursor.updateRow()
'''

field = 'STREET'
for field in [f.name for f in arcpy.Describe(lyr).fields if f.type == "String"]:  
    exp = "!{0}!.strip('N ')"  
    arcpy.management.CalculateField(lyr, field, exp.format(field), "PYTHON")
0 Kudos