5 Replies Latest reply on Jan 15, 2013 5:59 PM by KAldous

    Parse/Trim function in field calculator


      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,
        • Re: Parse/Trim function in field calculator
          Good question, see:

          Calculate Field examples
          Geodata » Data types » Tables

          ...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:]
          • Re: Parse/Trim function in field calculator
            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_!)
            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!
            • Re: Parse/Trim function in field calculator
              My indents did not show up with the copy/paste.
              • Re: Parse/Trim function in field calculator
                Okay, to paste code here you need the code tags, place [/CODE] 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_!): [CODE] 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'...

                • Re: Parse/Trim function in field calculator
                  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:


                  def RemoveZero(txtstr):
                    txtList = list(txtstr)
                    if (txtList[2] == '0'):
                      txtList2 = txtList[0], txtList[1], txtList[3]
                      txtstr = ''.join(txtList2)
                      return txtstr
                      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.