Select to view content in your preferred language

Using python to replace subtypes with descriptions

2255
2
Jump to solution
10-29-2014 07:07 AM
DirkPeterson
Occasional Contributor

I have a workflow that wil use a definition query to select a subset of point data that will be exported from SDE to a 3rd party.  One of the attributes is a subtype where there are 14 subtypes.  When I export the data, the subtype remains as a number rather than a description.  What I need to do is come up with a python expression to replace all of the different subtype numbers with the descriptions.  I have found where I can replace subtype with the description, but how do I do all of them at once?

This is what I came up with:

!Fieldname!.replace("subtype1","description1").("subtype2","description2")........("subtype14","description14)

When I run this, I get a ERROR 000539

I'm new to Python so any help would be appreciated.

0 Kudos
1 Solution

Accepted Solutions
DirkPeterson
Occasional Contributor

I just figured out between each I need to have .replace instead of just a .

View solution in original post

0 Kudos
2 Replies
DirkPeterson
Occasional Contributor

I just figured out between each I need to have .replace instead of just a .

0 Kudos
Zeke
by
Honored Contributor

What is the field type? If it's numeric (since you mention number), you can't put a string into it. Otherwise, I don't think you can run a multiple replace in one line like that; at least, I've never seen it.

Assuming this is in Field Calculator, set the parser to Python and check Show Codeblock. In Pre-Logic Script Code, enter something like:

def DoThis(myField):  


    if myfield == '1':


        val = 'description1'


    elif myfield == '2':


        val = 'description2'


    elif myfield == '3':


        val = 'description3'


    .


    .


    .


    elif myfield == '14':


        val = 'description14'


    else:    


        val = 'somedefaultvalue' 


    return val



In the Fieldname = box, enter


DoThis(!Fieldname!)



This assumes that Fieldname is text. If not, you'll have to add a new text field and calculate that instead.

There are other ways to handle this; you could add subtypes or domains to your new feature class, for example, but the above should work ok.

0 Kudos