-1

I'm trying to populate a field in a table using field calculator with the value of the field in another, concatenated with the last 6 digits of another field.Example below, I would like a field SUBZONE to be populated with the zone code U, along with the last 6 digits of objectID, so U123456

EG:

OBJID 000000006000123456

ZONE U

output new field:

U123456

A script will make this so easy.

closed as off-topic by PolyGeo Apr 23 '18 at 4:02

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions seeking help to debug/write/improve code must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Providing a clear problem statement and evidence of a code attempt will help others to help you. See: How to create a Minimal, Complete, and Verifiable example." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

  • You will not need an advanced calculation. With the python (or python 9.3) parser: SUBZONE ='U' + str(!OBJECTID!)[-6:], this means the string 'U' and the last 6 digits of the OBJECTID converted to a string, joined with simple concatenation. It becomes a little tricky if OBJECTID isn't 6 digits long, use zfill then.. – Michael Stimson Apr 23 '18 at 3:19
  • would this be correct fi SUBZONE is not always U? SUBZONE = '!ZONE!' + str(!OBJECTID!)[-6:] – jackiedaniels Apr 23 '18 at 3:28
  • Yes, the SUBZONE is implied by your field calculator so you only need to enter the second half.. if your OBJECTID is less than 6 digits then use 'U' + str(!OBJECTID!).zfill(6)[-6:], zfill has no effect if the string is already 6 or more characters but will pad to the left 0's to make 6 characters then [-6:] instructs python to take the rightmost 6 characters of the string only. If your subzone is not always U then where do you get that from? if it comes from your Zone field then the calc is !Zone! + str(!OBJECTID!).zfill(6)[-6:]. – Michael Stimson Apr 23 '18 at 3:32
  • Good to hear, I'll put that in as an answer to close this post off. – Michael Stimson Apr 23 '18 at 3:36
4

Your field calculation will be 'U' + str(!OBJECTID!).zfill(6)[-6:]. This has several components:

  • using str() we convert the value in OBJECTID to a string, this is required by the next step.
  • zfill() the string with 0's to the left to make at least 6 characters using zfill(6), this has no effect if the string has 6 or more characters already.
  • With the indexing notation [-6:] we extract the rightmost 6 digits, no matter how long the string is..

If your subzone is derived from a field then you can substitute that value using the calculation !Zone! + str(!OBJECTID!).zfill(6)[-6:] (assuming your subzone character comes from a field called Zone).

Not the answer you're looking for? Browse other questions tagged or ask your own question.