How can I remove certain text/string from all the rows in a filed with out having to do multiple updatcursors?
I need don't want to create another filed I just need the city and state removed from all the rows of a certain field.
|1||address city1 state1||address|
|2||address city2 State1||address|
|3||address city3 State1||address|
|4||address city2 State1||address|
|5||address city4 State1||address|
|6||address city5 State1||address|
|7||address city5 State1||address|
|8||address city1 State1||address|
|9||address city2 State1||address|
Was thinking something like this but I doesn't work.
fc = 'featureclass' tx = ['City1 State','City2 State','City3 State','City4 State', 'City5 State'] with arcpy.da.UpdateCursor(fc, ['field1']) as cursor: for row in cursor: if row[:13] in tx: row = row[:13] cursor.updateRow(row) del cursor
Potentially, you could lose your mind trying to figure out how many combinations of address, city and state your data has. Just sayin'...
But if you want the last n characters of a string you'd use:
myString = 'Some Street and City State' myString[-5:] #returns State
Aw I didn't think of that.
fc = 'featureclass' tx = ['City1 State','City2 State','City3 State','City4 State', 'City5 State'] with arcpy.da.UpdateCursor(fc, ['field1']) as cursor: for row in cursor: row = " ".join(row.split()[:-2]) cursor.updateRow(row) del cursor
I did notice that it did remove ones like
12345 StreetA St city State -->12345 StreetA St # What I wanted
0 StreetB St --> 0 # This is the issues, I would prefer if it just stayed 0 StreetB St.
I agree with Joe Borgione on several points. Firstly, actual sample data never hurts. Secondly, and more importantly, address parsing is notoriously complex, and there is no reason to reinvent wheels that already work well. There are several US address parsing libraries, I suggest you use one of those.