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 Dec 30 '14 at 18:00
  • I am using ArcGIS 10.2.2 – Jeff Timm Dec 30 '14 at 18:07

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 Dec 30 '14 at 18:27
  • Sorry, forgot a return statement. Bad cold, fuzzy thinking. Edited 2nd function, should work now. – recurvata Dec 30 '14 at 19:43
  • It looks like it should work but now it just returns the same values – Jeff Timm Dec 30 '14 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 Dec 30 '14 at 20:04
  • Edited for slightly better method, checking endswith(). – recurvata Dec 30 '14 at 20:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.