Parse/Trim function in field calculator

4866
5
Jump to solution
01-13-2013 09:33 PM
KristinAldous
New Contributor II
Hello,

I am new to python and coding in ArcGIS.

I am looking to remove a character at a specfic location in a string with the field calculator.

e.g. 1-01 needs to be 1-1

In this case it is always a 0 at the third location.  I know there are fairly simple parse and trim functions for this purpose but I havent been able to find a summary of these functions to figure it out (anyone know any help pages on these?)

Thanks in advance,
Kristin
Tags (2)
1 Solution

Accepted Solutions
T__WayneWhitley
Frequent Contributor
Okay, to paste code here you need the code tags, place after your code and
 before your code - I reversed them here so you could see what I mean (had I not done that, the part of my sentence in-between the tags would have been formatted as a block of code.  You originally expressed interest in the Python parser, so the below corrected code should work, using the expression, RemoveZero(!M_U_!): 
 def RemoveZero(txtStr):      txtLst = txtStr.split('-')      txtStr = txtLst[0] + '-' + str(int(txtLst[1]))      return txtStr


Don't forget to use the Python parser (check that box on).  Looks like you were mixing VBScript and Python conventions.
As a brief explanation, the exclamation marks (!) are a way to pass in field values in Python (the brackets, [], are used in VBScript).
The function is called by the expression, RemoveZero(!M_U_!), where M_U_ is the denoted field.  In the line of the code block declaring the function definition, def RemoveZero(txtStr), the 'txtStr' variable is a 'dummy' substitution for the value found in M_U_.  In other words the value is passed from M_U_ to txtStr where it is operated on in the function and returned as txtStr...it could have been returned as something else, but txtStr was convenient.  As long as the correct data type (and length, etc.) is returned, the field you are calculating will accept the value.

Hope that helps.  By the way, the last expression given you, !<your fieldname>![0:2] + !<your fieldname>![-1:], was for a calculation without the code block, also used with the Python parser.

If all's well, please mark this post as 'answered'...

Thanks,
Wayne

View solution in original post

0 Kudos
5 Replies
T__WayneWhitley
Frequent Contributor
Good question, see:

Calculate Field examples
Geodata » Data types » Tables
http://resources.arcgis.com/en/help/main/10.1/index.html#//005s0000002m000000

...particularly the heading Python built-in functions, under which there's a link to the Python site.  For your question on strings, you'll need to refer to the section 3.1.2. Strings.

So if your string formatting given in your example is constant throughout your table's field, then (using the Python parser in the field calculator), I believe you can do this (test it, replacing <your fieldname> with the applicable field name from your table):

!<your fieldname>![0:2] + !<your fieldname>![-1:]
0 Kudos
KristinAldous
New Contributor II
Thanks for the link.

I wasn't able to get the code you suggested to work though.  It doesnt return errors, just simply does not update the text.

My problem is a little more complex as well - only some entries have a zero in the third position. 

e.g. 1-01 should be 1-1 however 1-10 needs to remain 1-10.

I tried to write a Python code block in the field calculator to attempt to return the characters at 1,2, and 4, only if there is a 0 at character 3, however I have been having trouble getting the syntax for using field names and understanding when to use [ ], ! or ( ). (This code returns parse error at line 2).

Expression: RemoveZero( !M_U_!)
Code:
def RemoveZero( M_U_):
  if left ([ M_U_], 3) = 0:
    return left ([ M_U_], 1,2) + Mid ([ M_U_], 4, 1)

Any suggestions are much appreciated!
0 Kudos
KristinAldous
New Contributor II
My indents did not show up with the copy/paste.
0 Kudos
T__WayneWhitley
Frequent Contributor
Okay, to paste code here you need the code tags, place after your code and
 before your code - I reversed them here so you could see what I mean (had I not done that, the part of my sentence in-between the tags would have been formatted as a block of code.  You originally expressed interest in the Python parser, so the below corrected code should work, using the expression, RemoveZero(!M_U_!): 
 def RemoveZero(txtStr):      txtLst = txtStr.split('-')      txtStr = txtLst[0] + '-' + str(int(txtLst[1]))      return txtStr


Don't forget to use the Python parser (check that box on).  Looks like you were mixing VBScript and Python conventions.
As a brief explanation, the exclamation marks (!) are a way to pass in field values in Python (the brackets, [], are used in VBScript).
The function is called by the expression, RemoveZero(!M_U_!), where M_U_ is the denoted field.  In the line of the code block declaring the function definition, def RemoveZero(txtStr), the 'txtStr' variable is a 'dummy' substitution for the value found in M_U_.  In other words the value is passed from M_U_ to txtStr where it is operated on in the function and returned as txtStr...it could have been returned as something else, but txtStr was convenient.  As long as the correct data type (and length, etc.) is returned, the field you are calculating will accept the value.

Hope that helps.  By the way, the last expression given you, !<your fieldname>![0:2] + !<your fieldname>![-1:], was for a calculation without the code block, also used with the Python parser.

If all's well, please mark this post as 'answered'...

Thanks,
Wayne
0 Kudos
KristinAldous
New Contributor II
Thanks so much for the python help!

I was able to get the code working using the array rather than the left, mid functions.

Here's the code I used:

Expression:RemoveZero(!M_U_!):

def RemoveZero(txtstr):
  txtList = list(txtstr)
  if (txtList[2] == '0'):
    txtList2 = txtList[0], txtList[1], txtList[3]
    txtstr = ''.join(txtList2)
    return txtstr
  else:
    return txtstr


And of course I was using the wrong parser for the !<your fieldname>![0:2] + !<your fieldname>![-1:] expression.  It works fine and I'm sure will come in handy.

Thanks,
Kristin
0 Kudos