Python Help needed

648
11
Jump to solution
11-02-2017 12:09 PM
StevenFama
New Contributor II

I have a script that populates/updates a field based upon its content. If none of the conditions are true, I need it not to update / return <Null>. 

def Reclass(StoryHeight_Copy): 
if (StoryHeight_Copy == "1A"): 
return "1.25" 
elif (StoryHeight_Copy == "1H"): 
return "1.50" 
elif (StoryHeight_Copy == "1T"): 
return "1.75" 
elif (StoryHeight_Copy == "2A"): 
return "2.25"
elif (StoryHeight_Copy == "2H"): 
return "2.50" 
elif (StoryHeight_Copy == "2T"): 
return "2.75" 
elif (StoryHeight_Copy == "3A"): 
return "3.25"
elif (StoryHeight_Copy == "3H"): 
return "3.50" 
elif (StoryHeight_Copy == "3T"): 
return "3.75"

0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Esteemed Contributor

First, reformat your code then if add

else:
    return None‍‍‍‍

The above will return <null>

otherwise, if you don't want <null>

else:
    return "something else"‍‍

You have to return something, it is either None (aka ) or something

View solution in original post

11 Replies
DanPatterson_Retired
MVP Esteemed Contributor

First, reformat your code then if add

else:
    return None‍‍‍‍

The above will return <null>

otherwise, if you don't want <null>

else:
    return "something else"‍‍

You have to return something, it is either None (aka ) or something

StevenFama
New Contributor II

I want it to return what is in another field or just leave what is in the existing cell 

0 Kudos
StevenFama
New Contributor II

AS you can see, I created a field *_Copy.  when I run the script above, it works till the 'IF' is false. Then it updates to null.  I want it to leave what is already there 1, or 2, or what ever else is their. 

0 Kudos
curtvprice
MVP Esteemed Contributor

Is this Pro or ArcMap? Are you using the Calculate Field tool?

0 Kudos
StevenFama
New Contributor II

ArcMap using the field calc.

0 Kudos
curtvprice
MVP Esteemed Contributor

Sometimes https://community.esri.com/community/developers/gis-developers/python?sr=search&searchId=05a9d169-b0... is simpler than one thinks.

Like Dan said, try:

else:
    return StoryHeight_Copy
StevenFama
New Contributor II

Thank you.  That worked!!!!

0 Kudos
XanderBakker
Esri Esteemed Contributor

Just an additional comment to put in my two cents.

When the list of possibilities gets a bit long you can use dictionaries: See the example below:

def Reclass(story_height):
    dct = {"1A": "1.25", "1H": "1.50", "1T": "1.75",
           "2A": "2.25", "2H": "2.50", "2T": "2.75",
           "3A": "3.25", "3H": "3.50", "3T": "3.75"}
    if story_height in dct:
        return dct[story_height]
    else:
        return story_height

 This allows you to search for a key in the dictionary and if it exists it will return the corresponding value. 

Since I notice that there is a logic in the values returned, in this case you could also do this:

def Reclass2(story_height):
    dct = {"A": ".25", "H": ".50", "T": ".75"}
    result = story_height
    if len(story_height) == 2:
        if story_height[1] in dct:
            result = story_height[0] + dct[story_height[1]]
    return result

The A, H and T value represent the value (ok, text) behind the decimal and just put the first character in front of it.

When you run it (as shown below in a snippet of Python, not the Field Calculator) you can see the result:

def main():
    lst_story_height = ["2H", "3T", "1A", "4H", "5A", "6T"]

    print "\nOriginal methode: Reclass:"
    for story_height in lst_story_height:
        print story_height, Reclass(story_height)

    print "\nAlternative methode: Reclass2:"
    for story_height in lst_story_height:
        print story_height, Reclass2(story_height)


def Reclass(story_height):
    dct = {"1A": "1.25", "1H": "1.50", "1T": "1.75",
           "2A": "2.25", "2H": "2.50", "2T": "2.75",
           "3A": "3.25", "3H": "3.50", "3T": "3.75"}
    if story_height in dct:
        return dct[story_height]
    else:
        return story_height


def Reclass2(story_height):
    dct = {"A": ".25", "H": ".50", "T": ".75"}
    result = story_height
    if len(story_height) == 2:
        if story_height[1] in dct:
            result = story_height[0] + dct[story_height[1]]
    return result

if __name__ == '__main__':
    main()

This will yield:

Original methode: Reclass:
2H 2.50
3T 3.75
1A 1.25
4H 4H
5A 5A
6T 6T

Alternative methode: Reclass2:
2H 2.50
3T 3.75
1A 1.25
4H 4.50
5A 5.25
6T 6.75
carlanderson2
New Contributor

your code could be shorter and cleaner as

def Reclass(StoryHeight_Copy):
   d = {"1A":"1.25","1H":"1.5","1T":"1.75"....}
   if StoryHeight_Copy in d:
        return d[StoryHeight_Copy]
   return None