Solved! Go to Solution.
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
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
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