Table
I am fairly new to using python in field calculator and could use some help. I want to populate SitusRdType with RD or AVE or LN or ST or DR depending on the road type. I also want to just have the street name in SitusSt. Any thoughts on how to go about this?
-
What software are you suing (QGIS, ArcGIS, other)?recurvata– recurvata2014年12月30日 18:00:28 +00:00Commented Dec 30, 2014 at 18:00
-
I am using ArcGIS 10.2.2Jeff Timm– Jeff Timm2014年12月30日 18:07:38 +00:00Commented Dec 30, 2014 at 18:07
1 Answer 1
In general, you need a pre-defined list of the road types, since not all your values end in a road type. For ArcGIS, the following is a simple example of populating SitusRdType:
In the Field Calculator, check that Python is set as the parser and check Show Codeblock. In the Pre-Logic Script Code box:
def setRdType(myInput):
lstRdTypes = [' RD', ' AVE', ' LN', ' ST', ' DR']
for rdType in lstRdTypes:
if rdType in myInput:
return rdType
In the SitusRdType = box: setRdType(!SitusSt!) You may want to add 'HWY ' to the list if you want that as well. There's probably a more elegant way, but off the top of my head this should work.
To eliminate the same list from SitusSt:
def rplc(myInput):
s = myInput
lstRdTypes = [' RD', ' AVE', ' LN', ' ST', ' DR']
for rdType in lstRdTypes:
if s.endswith(rdType):
s = s.replace(rdType, '')
return s
In SitusSt =: rplcType(!SitusSt!)
Again, probably more efficient ways to do it, but this should work. Always try on a copy of data first.
-
The first part worked great once I changed [SitusSt] to !SitusSt! thank you! The second part just returned null values.Jeff Timm– Jeff Timm2014年12月30日 18:27:59 +00:00Commented Dec 30, 2014 at 18:27
-
Sorry, forgot a return statement. Bad cold, fuzzy thinking. Edited 2nd function, should work now.recurvata– recurvata2014年12月30日 19:43:25 +00:00Commented Dec 30, 2014 at 19:43
-
It looks like it should work but now it just returns the same valuesJeff Timm– Jeff Timm2014年12月30日 19:52:02 +00:00Commented Dec 30, 2014 at 19:52
-
Hmm, code looks good. Edited for a slightly different approach, try that. You could also skip the codeblock and do a replace() on each road type separately, but that's obviously a less efficient method.recurvata– recurvata2014年12月30日 20:04:12 +00:00Commented Dec 30, 2014 at 20:04
-
Edited for slightly better method, checking endswith().recurvata– recurvata2014年12月30日 20:20:31 +00:00Commented Dec 30, 2014 at 20:20
Explore related questions
See similar questions with these tags.