Snip of my Calculate Field window with code block and expression.

I'm trying to use values from a field to fill a new field with a truncated version. The 'str.rindex' function is working out well, but I can't seem to figure out how I'm supposed to reference the field I need truncated. I found a few answers on here explaining that the !field! convention won't work, but no solutions for a workaround.


in the code block you'll define a function, and then in the expression, you'll call the function with the needed field names, like so :

Code block :

def picture_name(current_name, map_number):
    return current_name[current_name.rindex("\\"):] + "|" + map_number + ".tif"

Expression :

picture_name(!Pictures!, !MapNumber!)

I haven't test this code, and I rewrote a bit of the logic. Hopefully this at least gets you closer.

| improve this answer | |
  • This looks good to me, I'll give it a shot and report back. Thanks much. – Ryan H Oct 30 '14 at 15:29
  • This appears to be working. The final result is pulling the fields just fine. The truncation is getting a bit off, but that was not part of my original question. As far as I'm concerned this answer was a great success. Things to watch out for this needs everything to be in a string field, so watch what you're pulling from if you are trying to do a similar function. Thanks again for the help. – Ryan H Oct 30 '14 at 16:05
  • 1
    I should have figured MapNumber might not be a string field. You can just change it to... + "|" + str(map_number) + ".tif" – MWrenn Oct 30 '14 at 16:18

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.