Select to view content in your preferred language

Renaming values from one field into another.

568
4
10-24-2013 11:58 AM
CoyPotts1
Deactivated User
Hello,

I am very new to Python scripting and I am trying to write one that will take values (they are always consistent), and based on their value enter a string into another field.  I am working with data that is always coded the same (referenced below as field1, and I want to take those codes and automate a way to populate a different field based on those codes. 

I have been trying to play with this, but it hasn't worked so far:

Expression:
Reclass(!FIELD1!)

Code block:
def Reclass(FIELD1):
  if (FIELD1 == "H3010"):
    return "River"

Ideally this is supposed to end up being an expression that the user can just load into the field calculator and it will grab the values from FIELD1 and place the desired values into FIELD2.
Tags (2)
0 Kudos
4 Replies
ChrisPedrezuela
Frequent Contributor
def Reclass(FIELD1):
  if FIELD1 == "H3010":
    return "River"
  elif FIELD1 == "ABC":
    return "Something"


Hello,

I am very new to Python scripting and I am trying to write one that will take values (they are always consistent), and based on their value enter a string into another field.  I am working with data that is always coded the same (referenced below as field1, and I want to take those codes and automate a way to populate a different field based on those codes. 

I have been trying to play with this, but it hasn't worked so far:

Expression:
Reclass(!FIELD1!)

Code block:
def Reclass(FIELD1):
  if (FIELD1 == "H3010"):
    return "River"

Ideally this is supposed to end up being an expression that the user can just load into the field calculator and it will grab the values from FIELD1 and place the desired values into FIELD2.
0 Kudos
CoyPotts1
Deactivated User
Okay, thanks for that input, Thanos. 

I have decided that an actual script within a toolbox is the route that I am going to go.  I have this worked out so far:

import arcpy

# Set the input workspace
#
arcpy.env.workspace = arcpy.GetParameterAsText(0)

# Set the output workspace
#
outWorkspace = arcpy.GetParameterAsText(1)

try:
    # Get a list of the featureclasses in the input folder
    #
    fcs = arcpy.ListFeatureClasses()
    
    for fc in fcs:   
        # Validate the new feature class name for the output workspace.
        #
        featureClassName = arcpy.ValidateTableName(fc, outWorkspace)
        outFeatureClass = os.path.join(outWorkspace, featureClassName)


def Reclass(symbol):
  if MTFCC == "H3010":
    return "River"
  elif MTFCC == "H1100":
    return "Creek"
    

except:
    arcpy.AddMessage(arcpy.GetMessages(2))
    print arcpy.GetMessages(2)


Basically I am to the point where I tell it what to do and I'm not really sure how to do so.
0 Kudos
ChrisPedrezuela
Frequent Contributor
Hi buddy,

Just look thru this documentation to have a feel for what you want to do,

http://resources.arcgis.com/en/help/main/10.1/index.html#//00170000004m000000

Cheers

Okay, thanks for that input, Thanos. 

I have decided that an actual script within a toolbox is the route that I am going to go.  I have this worked out so far:

import arcpy

# Set the input workspace
#
arcpy.env.workspace = arcpy.GetParameterAsText(0)

# Set the output workspace
#
outWorkspace = arcpy.GetParameterAsText(1)

try:
    # Get a list of the featureclasses in the input folder
    #
    fcs = arcpy.ListFeatureClasses()
    
    for fc in fcs:   
        # Validate the new feature class name for the output workspace.
        #
        featureClassName = arcpy.ValidateTableName(fc, outWorkspace)
        outFeatureClass = os.path.join(outWorkspace, featureClassName)


def Reclass(symbol):
  if MTFCC == "H3010":
    return "River"
  elif MTFCC == "H1100":
    return "Creek"
    

except:
    arcpy.AddMessage(arcpy.GetMessages(2))
    print arcpy.GetMessages(2)


Basically I am to the point where I tell it what to do and I'm not really sure how to do so.
0 Kudos
CoyPotts1
Deactivated User
That was helpful.  Thanks again, Thanos!

I think this code is getting me closer to what I need, but I am still getting syntax errors.  Since I'm using example code blocks to copy/paste, I feel like I may be putting more into this than needed.  The user will have an open ArcMap document that will have one or two feature classes in it.  The feature classes won't always have the same name, so I would like for the input features/table to be requested, and not just signified within the code. 

Secondly, do I need to have the lines that get the input and output workspace if I am running straight from the document that I am wanting the information written to? 

Here is the code that I have gotten so far:

import arcpy

# Set the input workspace
arcpy.env.workspace = arcpy.GetParameterAsText(0)

# Set the output workspace
outWorkspace = arcpy.GetParameterAsText(1)

try: 

    # Get a list of the featureclasses in the input folder
    fcs = arcpy.ListFeatureClasses()
    
    for fc in fcs:   
        # Validate the new feature class name for the output workspace.
        featureClassName = arcpy.ValidateTableName(fc, outWorkspace)
        outFeatureClass = os.path.join(outWorkspace, featureClassName)
 
    # Set local variables
    inTable = arcpy.GetParameterAsText(2)
    fieldName1 = "symbol"
    expression = "Reclass(string(!MTFCC!))"
    codeblock = """def Reclass(symbol):
      if MTFCC == "H3010":
        return "River"
      elif MTFCC == "H1100":
        return "Creek""""

    # Add fields
    arcpy.AddField_management(inTable, fieldName1, "STRING")
    
 
    # Calculate symbol values
    arcpy.CalculateField_management(inTable, fieldName1, expression, "PYTHON_9.3", codeblock)
    
except:
    arcpy.AddMessage(arcpy.GetMessages(2))
    print arcpy.GetMessages(2)
0 Kudos