AnsweredAssumed Answered

Python - using Replace in Field Calculator

Question asked by cdspatial Champion on Jan 29, 2015
Latest reply on Jan 29, 2015 by cdspatial

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".




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 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 =






Thanks in advance,


Chris Donohue, GISP