1

I have a script tool that truncates each word in field to 3 characters and outputs the result in a new field:

"THIS IS AN EXAMPLE STRING" becomes "THI IS AN EXA STR"

import arcpy
arcpy.env.overwriteOutput = True

# Prelogic used to pass through for calculating truncated name values
codeblock = """
def nmshort(stname):
  s = stname.split()
  s2 = s[:] = [elem[:3] for elem in s]
  return ' '.join(s2)
""" # Finished code block

try:
    # Set input parameters
    inFC = arcpy.GetParameterAsText(0)
    inName = arcpy.GetParameterAsText(1)
    inNameShort = arcpy.GetParameterAsText(2)

    # set expression for field calculation to variables
    expression = "nmshort(!{0}!)".format(inName)

    # perform field calculation
    arcpy.AddMessage("Calculating truncated strings for values in input name field")
    arcpy.CalculateField_management(inFC, inNameShort, expression, "PYTHON_9.3", codeblock)
    arcpy.AddMessage("Calculation complete")

except:
    arcpy.AddError("Could not complete the calculation")

    arcpy.AddMessage(arcpy.GetMessages())

It works fine except I want to make a parameter in the tool that will allow the user to specify the length for truncation instead of a constant of 3. With having to pass the codeblock to calculatefield as a string, I'm struggling to figure out how to pass the length parameter to a variable in the codeblock. Most of the time the length will be either 3 or 4, so I suppose I could create two blocks of code: one to run if the length parameter is 3 and another if 4. That would be the easy way out though and I want to learn (I'm a noob). I'm really interested in doing this with an update cursor, if it can be done that way. I've played with cursors with some success with simpler calculations. I can't figure out how to write the more advanced logic involved in this calculation though.

I'm using ArcGIS 10.0 so I can't use the data access cursors.

2

This is a pure Python example to show you the principle:

def nmshort(stname,stnum):
  s = stname.split()
  s2 = s[:] = [elem[:stnum] for elem in s]
  return ' '.join(s2)

exString = "THIS IS AN EXAMPLE STRING"
exNumber = 2

print nmshort(exString,exNumber)

This is your code adapted, but untested - I am assuming that you do not yet have the fourth parameter and that you will be passing through an integer (which I'll grab as text and convert back to an integer rather than use GetParameter).

import arcpy
arcpy.env.overwriteOutput = True

# Prelogic used to pass through for calculating truncated name values
codeblock = """
def nmshort(stname,stnum):
  s = stname.split()
  s2 = s[:] = [elem[:stnum] for elem in s]
  return ' '.join(s2)
""" # Finished code block

try:
    # Set input parameters
    inFC = arcpy.GetParameterAsText(0)
    inName = arcpy.GetParameterAsText(1)
    inNameShort = arcpy.GetParameterAsText(2)
    inNumber = arcpy.GetParameterAsText(3)

    # set expression for field calculation to variables
    expression = "nmshort(!{0}!,{1})".format(inName,int(inNumber))

    # perform field calculation
    arcpy.AddMessage("Calculating truncated strings for values in input name field")
    arcpy.CalculateField_management(inFC, inNameShort, expression, "PYTHON_9.3", codeblock)
    arcpy.AddMessage("Calculation complete")

except:
    arcpy.AddError("Could not complete the calculation")

    arcpy.AddMessage(arcpy.GetMessages())
  • The above results in ERROR 000539: Invalid Field 4. 4 was the value of the input parameter for the truncation length. It looks like the length parameter is getting interpreted as a field: ...SiteAddress CPSHORT nmshort(!PLACENAME!,!4!)... I set inNumber as a Long in the script tool. Should I have done something else? – Hoss Sep 19 '15 at 17:41
  • @Hoss I think the changes I made make this worth another try. – PolyGeo Sep 19 '15 at 21:01
  • I tried a similar adjustment but I didn't convert the inNumber to integer in the codeblock. I'll give it another go. It's always worth a try. Thanks. – Hoss Sep 19 '15 at 21:05
  • @ PolyGeo - That did the trick. To wrap my mind around how this worked: basically, we made the length parameter look like a field to CalculateField and set the values of that field to whatever number the user inputs? I'm still learning the etiquette of this site. I'll create a new question for the cursor approach. Thanks! – Hoss Sep 21 '15 at 16:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.