Convert Excel formula to Python

4123
3
Jump to solution
07-05-2021 09:58 AM
GISCityofPrinceAlbert
New Contributor II

Hello, I am fairly new to Python and just started working with ArcPro.  I have a solution for my problem working in an excel formula but would like to know how to get this to work in Code Block of Pro.

I have 2 fields; Location and Section.  My excel formula is this: 

=IF((LEFT(A2,3)="RMS"),"",REPLACE(A2,1,1,B2))

A2 is location and B2 is Section.  So if the 1st 3 characters of the location = RMS then nothing else replace the first character with Section

The data looks like this

LocationSectionFormula 
B - 18 - F - 1 - N.5AA - 18 - F - 1 - N.5
RMS - 18 - F - 1 - N.6RMS 

 

I have been searching all over but just can't seem to figure it out. 

Thank you. 

0 Kudos
1 Solution

Accepted Solutions
GISCityofPrinceAlbert
New Contributor II

I think I figured it out, thank you for the help.

GISCityofPrinceAlbert_0-1625517092799.png

 

View solution in original post

3 Replies
DanPatterson
MVP Esteemed Contributor
def replace_(fld, val):
    """Replace a value if found"""
    if fld[:3] == val:
        return val
    else:
        return fld[:3]

 

python expression 

replace_(!Location!, "RMS")

code block is above

However... you really don't say what you want to do it the Location field isn't really RMS ???


... sort of retired...
GISCityofPrinceAlbert
New Contributor II

Ideally I would like to update the location field and not create a new field. So if location starts with RMS then do nothing but if the 1st character in Location does not equal Section then update the 1st character in location to change to Section value.  Hopefully that makes sense. Thank you!

 

0 Kudos
GISCityofPrinceAlbert
New Contributor II

I think I figured it out, thank you for the help.

GISCityofPrinceAlbert_0-1625517092799.png