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
Solved! Go to Solution.
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
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
can you explain what lines 8 & 9 are doing please?
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.
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?
Can you give a few sample addresses, especially ones you are having issues with?
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")