+ Reply to Thread
Results 1 to 6 of 6

Thread: Parse/Trim function in field calculator

  1. #1
    Kristin Aldous
    Join Date
    May 2011
    Posts
    17
    Points
    0
    Answers Provided
    0


    0

    Default Parse/Trim function in field calculator

    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

  2. #2
    T. Wayne Whitley

    Join Date
    Apr 2010
    Posts
    1,027
    Points
    843
    Answers Provided
    118


    0

    Default Re: Parse/Trim function in field calculator

    Good question, see:

    Calculate Field examples
    Geodata » Data types » Tables
    http://resources.arcgis.com/en/help/...0000002m000000

    ...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:]

  3. #3
    Kristin Aldous
    Join Date
    May 2011
    Posts
    17
    Points
    0
    Answers Provided
    0


    0

    Default 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_!)
    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!

  4. #4
    Kristin Aldous
    Join Date
    May 2011
    Posts
    17
    Points
    0
    Answers Provided
    0


    0

    Default Re: Parse/Trim function in field calculator

    My indents did not show up with the copy/paste.

  5. #5
    T. Wayne Whitley

    Join Date
    Apr 2010
    Posts
    1,027
    Points
    843
    Answers Provided
    118


    0
    This post is marked as the answer

    Default Re: Parse/Trim function in field calculator

    Okay, to paste code here you need the code tags, place [/CODE] after your code and [CODE] 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'...

    Thanks,
    Wayne

  6. #6
    Kristin Aldous
    Join Date
    May 2011
    Posts
    17
    Points
    0
    Answers Provided
    0


    0

    Default 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:

    Expression:RemoveZero(!M_U_!):

    Code:
    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

+ Reply to Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts