Python - using Replace in Field Calculator

7734
9
Jump to solution
01-29-2015 08:53 AM
ChrisDonohue__GISP
MVP Alum

I have a beginning Python question I was hoping could be answered.  I'm trying to use Replace to change out street suffixes in a field [FacilityAddress] as there are many inconsistencies with how they were entered in a dataset that we need to clean up.  For example, there can be address entries with "STREET", "ST.", and "ST", and we need them all to be standardized to "ST".

address_wierdness.png

While I can individually make each Replace work by doing one statement at a time in Field Calculator, I am hoping to find a way to run them all at once.

However, when I try to run them all at once, I get an "Invalid Syntax in Line 1" error.

So my questions are this:

1.  I'm assuming the code to run all the replaces at once needs to go in the "Pre-logic Script Code" area, correct?

2.  Do I need the "def" (definition statement) when using Replace?

3.  Do I need to add "If Then Else" branching?

3.  Is the import.re (import regexes) necessary?

4.  Do I need to change the code to prevent the replacement from changing out non-suffixes?  For example, I have an address of "699 SHASTA ST RM 1,2,3" (see image above).  Will the replacement see the "ST" in the street name "SHASTA and operate on it.  I know it's probably moot in this specific case,  but I would like to know should we do a diffferent suffix selection.  For example, if we want to change all the streets to "STREET", would "SHASTA" become "SHASTREETTA"?

5.  What is causing the syntax error in line 1?

Here's the code I came up with so far:

----------------

def StreetSuffix(!FacilityAddress!):

import re

    !FacilityAddress!.replace("ALLEY", "AL")

    !FacilityAddress!.replace("AL.", "AL")

    !FacilityAddress!.replace("AVENUE", "AV")

    !FacilityAddress!.replace("AVE.", "AV")

    !FacilityAddress!.replace("AVE", "AV")

    !FacilityAddress!.replace("AV.", "AV")

    !FacilityAddress!.replace("BOULEVARD", "BL")

    !FacilityAddress!.replace("BLVD", "BL")

    !FacilityAddress!.replace("BL.", "BL")

    !FacilityAddress!.replace("CIRCLE", "CI")

    !FacilityAddress!.replace("CI., "CI")

    !FacilityAddress!.replace("COURT", "CT")

    !FacilityAddress!.replace("CT.", "CT")

    !FacilityAddress!.replace("DRIVE", "DR")

    !FacilityAddress!.replace("DR.", "DR")

    !FacilityAddress!.replace("HILL", "HL")

    !FacilityAddress!.replace("HL.", "HL")

    !FacilityAddress!.replace("LANE", "LN")

    !FacilityAddress!.replace("LN.", "LN")

    !FacilityAddress!.replace("LOOP", "LP")

    !FacilityAddress!.replace("LP.", "LP")

    !FacilityAddress!.replace("PLACE", "PL")

    !FacilityAddress!.replace("PL.", "PL")

    !FacilityAddress!.replace("PARKWAY", "PW")

    !FacilityAddress!.replace("PKWY", "PW")

    !FacilityAddress!.replace("PLAZA", "PZ")

    !FacilityAddress!.replace("PZ.", "PZ")

    !FacilityAddress!.replace("ROAD", "RD")

    !FacilityAddress!.replace("RD.", "RD")

    !FacilityAddress!.replace("SQUARE", "SQ")

    !FacilityAddress!.replace("SQ.", "SQ")

    !FacilityAddress!.replace("STREET", "ST")

    !FacilityAddress!.replace("ST.", "ST")

    !FacilityAddress!.replace("WAY", "WY")

    !FacilityAddress!.replace("WY.", "WY")

    return StreetSuffix

----------

FacilityAddress =

StreetSuffix(!FacilityAddress!)

--------

Thanks in advance,

Chris Donohue, GISP

0 Kudos
1 Solution

Accepted Solutions
DarrenWiens2
MVP Honored Contributor

Sorry, Chris. That was nonworking, untested code. I believe something like this should work:

def StreetSuffix(fa):
    list = fa.split()
    for i in range(len(list)):
        if list == 'RD':
            list = list.replace("RD","ROAD")
        if list == 'TRAIL':
            list = list.replace("TRAIL","TR")
      ....and so on
    return " ".join(list)

This should work, however, I see now that it would probably be much more concise to use a dictionary.

View solution in original post

9 Replies
DarrenWiens2
MVP Honored Contributor

Your expression is correct (StreetSuffix(!FacilityAddress!)), but nothing in the code block should have '!'s.

def StreetSuffix(FacilityAddress):
  newString = FacilityAddress.replace('ALLEY','AL')
  newString = newString.replace('AL.', 'AL')
  newString = newString.replace('AVENUE', 'AV')
  ....and so on
  return newString

I made some slight changes to the logic - I think it should work. You need to return the variable, not the function. And no need to import a library unless you use it. There are no calls to anything like re.someFunction(), so you know you don't need to import the re library.

EDIT 1: yes, this type of code will replace the 'ST' in 'SHASTA'. Instead, you could include the spaces in the replace call, like:

newString = FacilityAddress.replace(' ST ', ' STREET ')

This will only replace 'ST' between two spaces. You could do something like this to get at ' ST' at the end of the string:

if FacilityAddress[3:] == ' ST':
    FacilityAddress.replace(' ST', ' STREET')

Note that this will trip up in cases like "JOHN STEINBECK ST" -> "JOHN STREETEINBECK STREET"

There are probably a multitude of other gotchas, so unfortunately you'll have to fine-tune along the way.

EDIT 2: Now that I think of it, you're probably better off splitting the string (and later, joining back together) into a list and inspecting them individually:

def StreetSuffix(FacilityAddress)
   list = FacilityAddress.split()
   for item in list:
       item.replace('ALLEY', 'AL')
       item.replace('AL.', 'AL')
   return " ".join(list)
ChrisDonohue__GISP
MVP Alum

Thanks for the input Darren.  Let me edit the code and give it a try.

Chris Donohue, GISP

0 Kudos
ChrisDonohue__GISP
MVP Alum

I edited the code to change the double-quotes to single quotes and removed the ! ! from around the FacilityAddress in the code block.  In an attempt to refine the replacement to not replace non-Suffixes by accident, I compromised on the spacing for the strings by using a space in front of the suffix but not behind it.

However, when I run it, I get an error:

suffixreplace_error.png

Does 'newString' need to be defined as a variable?  How does Python handle that?

Chris Donohue, GISP

0 Kudos
ChrisDonohue__GISP
MVP Alum

Here's the code:

def StreetSuffix(FacilityAddress):

  newSting = FacilityAddress.replace(' ALLEY', ' AL')

  newSting = FacilityAddress.replace(' AL.', ' AL')

  newSting = FacilityAddress.replace(' AVENUE', ' AV')

  newSting = FacilityAddress.replace(' AVE.', ' AV')

  newSting = FacilityAddress.replace(' AVE', ' AV')

  newSting = FacilityAddress.replace(' AV.', ' AV')

  newSting = FacilityAddress.replace(' BOULEVARD', ' BL')

  newSting = FacilityAddress.replace(' BLVD', ' BL')

  newSting = FacilityAddress.replace(' BL.', ' BL')

  newSting = FacilityAddress.replace(' CIRCLE', ' CI')

  newSting = FacilityAddress.replace(' CI.', ' CI')

  newSting = FacilityAddress.replace(' COURT', ' CT')

  newSting = FacilityAddress.replace(' CT.', ' CT')

  newSting = FacilityAddress.replace(' DRIVE', ' DR')

  newSting = FacilityAddress.replace(' DR.', ' DR')

  newSting = FacilityAddress.replace(' HILL', ' HL')

  newSting = FacilityAddress.replace(' HL.', ' HL')

  newSting = FacilityAddress.replace(' LANE', ' LN')

  newSting = FacilityAddress.replace(' LN.', ' LN')

  newSting = FacilityAddress.replace(' LOOP', ' LP')

  newSting = FacilityAddress.replace(' LP.', ' LP')

  newSting = FacilityAddress.replace(' PLACE', ' PL')

  newSting = FacilityAddress.replace(' PL.', ' PL')

  newSting = FacilityAddress.replace(' PARKWAY', ' PW')

  newSting = FacilityAddress.replace(' PKWY', ' PW')

  newSting = FacilityAddress.replace(' PLAZA', ' PZ')

  newSting = FacilityAddress.replace(' PZ.', ' PZ')

  newSting = FacilityAddress.replace(' ROAD', ' RD')

  newSting = FacilityAddress.replace(' RD.', ' RD')

  newSting = FacilityAddress.replace(' SQUARE', ' SQ')

  newSting = FacilityAddress.replace(' SQ.', ' SQ')

  newSting = FacilityAddress.replace(' STREET', ' ST')

  newSting = FacilityAddress.replace(' ST.', ' ST')

  newSting = FacilityAddress.replace(' WAY', ' WY')

  newSting = FacilityAddress.replace(' WY.', ' WY')

  return newString

0 Kudos
ChrisDonohue__GISP
MVP Alum

DOH!  Now I see it - of course after I posted it.  Typo in "newString" - I have it in the code block as "newSting" (r missing).

Let me fix that and try it again.

Chris Donohue, GISP

0 Kudos
ChrisDonohue__GISP
MVP Alum

In regards to the splitting the string idea [EDIT 2], how are individual strings recognized?  Does the "split" see the spaces as limits of a string?

Chris Donohue, GISP

0 Kudos
ChrisDonohue__GISP
MVP Alum

I modified the code to Darren's List/Split idea [EDIT2] and it ran without error, but no changes appear to made to the suffixes.  Here's the Python code:

def StreetSuffix(FacilityAddress):

  list = FacilityAddress.split()

  for item in list:

    item.replace('ALLEY', 'AL')

    item.replace('AL.', 'AL')

    item.replace('AVENUE', 'AV')

    item.replace('AVE.', 'AV')

    item.replace('AVE', 'AV')

    item.replace('AV.', 'AV')

    item.replace('BOULEVARD', 'BL')

    item.replace('BLVD', 'BL')

    item.replace('BL.', 'BL')

    item.replace('CIRCLE', 'CI')

    item.replace('CI.', 'CI')

    item.replace('COURT', 'CT')

    item.replace('CT.', 'CT')

    item.replace('DRIVE', 'DR')

    item.replace('DR.', 'DR')

    item.replace('HILL', 'HL')

    item.replace('HL.', 'HL')

    item.replace('LANE', 'LN')

    item.replace('LN.', 'LN')

    item.replace('LOOP', 'LP')

    item.replace('PLACE', 'PL')

    item.replace('PL.', 'PL')

    item.replace('PARKWAY', 'PW')

    item.replace('PKWY', 'PW')

    item.replace('PLAZA', 'PZ')

    item.replace('PZ.', 'PZ')

    item.replace('RD.', 'RD')

    item.replace('SQ.', 'SQ')

    item.replace('STREET', 'ST')

    item.replace('ST.', 'ST')

    item.replace('WAY', 'WY')

    item.replace('WY.', 'WY')

  return " ".join(list)

Did I forget something?  Is there a way to tell if the issue is that it didn't detect anything to replace, or that it did try to replace stuff but the process is not finalizing that correctly?

Chris Donohue, GISP

0 Kudos
DarrenWiens2
MVP Honored Contributor

Sorry, Chris. That was nonworking, untested code. I believe something like this should work:

def StreetSuffix(fa):
    list = fa.split()
    for i in range(len(list)):
        if list == 'RD':
            list = list.replace("RD","ROAD")
        if list == 'TRAIL':
            list = list.replace("TRAIL","TR")
      ....and so on
    return " ".join(list)

This should work, however, I see now that it would probably be much more concise to use a dictionary.

ChrisDonohue__GISP
MVP Alum

Coolness - that worked!  Thanks for your expertise, Darren.  This is not only helped me solve a specific issue but has given me more understanding on how Python can be used.

Here's the final code that worked:

def StreetSuffix(FacilityAddress):

  list = FacilityAddress.split()

  for item in range(len(list)):

    if list[item] == 'ALLEY':

      list[item] = list[item].replace("ALLEY", "AL")   

    if list[item] == 'AL.':

      list[item] = list[item].replace("AL.", "AL")

    if list[item] == 'AVENUE':

      list[item] = list[item].replace("AVENUE", "AV")   

    if list[item] == 'AVNUE':

      list[item] = list[item].replace("AVNUE", "AV")    

    if list[item] == 'AVE.':

      list[item] = list[item].replace("AVE.", "AV")   

    if list[item] == 'AVE':

      list[item] = list[item].replace("AVE", "AV")

    if list[item] == 'BOULEVARD':

      list[item] = list[item].replace("BOULEVARD", "BL")   

    if list[item] == 'BLVD':

      list[item] = list[item].replace("BLVD", "BL")

    if list[item] == 'BL.':

      list[item] = list[item].replace("BL.", "BL")   

    if list[item] == 'CIRCLE':

      list[item] = list[item].replace("CIRCLE", "CI")

    if list[item] == 'CI.':

      list[item] = list[item].replace("CI.", "CI")   

    if list[item] == 'COURT':

      list[item] = list[item].replace("COURT", "CT")

    if list[item] == 'CT.':

      list[item] = list[item].replace("CT.", "CT")   

    if list[item] == 'DRIVE':

      list[item] = list[item].replace("DRIVE", "DR")

    if list[item] == 'DR.':

      list[item] = list[item].replace("DR.", "DR")   

    if list[item] == 'HILL':

      list[item] = list[item].replace("HILL", "HL")

    if list[item] == 'HL.':

      list[item] = list[item].replace("HL.", "HL")   

    if list[item] == 'LANE':

      list[item] = list[item].replace("LANE", "LN")

    if list[item] == 'LN.':

      list[item] = list[item].replace("LN.", "LN")   

    if list[item] == 'LOOP':

      list[item] = list[item].replace("LOOP", "LP")

    if list[item] == 'PLACE':

      list[item] = list[item].replace("PLACE", "PL")   

    if list[item] == 'PL.':

      list[item] = list[item].replace("PL.", "PL")

    if list[item] == 'PARKWAY':

      list[item] = list[item].replace("PARKWAY", "PW")   

    if list[item] == 'PKWY':

      list[item] = list[item].replace("PKWY", "PW")

    if list[item] == 'PLAZA':

      list[item] = list[item].replace("PLAZA", "PZ")   

    if list[item] == 'PZ.':

      list[item] = list[item].replace("PZ.", "PZ")

    if list[item] == 'ROAD':

      list[item] = list[item].replace("ROAD", "RD")   

    if list[item] == 'RD.':

      list[item] = list[item].replace("RD.", "RD")

    if list[item] == 'SQUARE':

      list[item] = list[item].replace("SQUARE", "SQ")   

    if list[item] == 'SQ.':

      list[item] = list[item].replace("SQ.", "SQ")

    if list[item] == 'STREET':

      list[item] = list[item].replace("STREET", "ST")

    if list[item] == 'STR.':

      list[item] = list[item].replace("STR.", "ST")   

    if list[item] == 'ST.':

      list[item] = list[item].replace("ST.", "ST")

    if list[item] == 'WAY':

      list[item] = list[item].replace("WAY", "WY")   

    if list[item] == 'WY.':

      list[item] = list[item].replace("WY.", "WY")

  return " ".join(list)

Chris Donohue, GISP

0 Kudos