0

I'm using field calculator in ArcGIS Desktop 10.2.1.

I have field1 which has a values of "A_B" (example). I created two fields out of this, values to the left of the underscore go in fielda and to the right of the underscore go in fieldb. I used the code below in field calculator to separate them out into two fields.

fielda =

field1.split("_")[-1]

fieldb =

`field1.split("_")[-1]`

I now have field1 with values of A_B_C_D. I need to separate the A_B_C_D into four separate fields using field calculator. fielda = A, fieldb = B, fieldc = C and so on. I'm not sure how to go about this. I basically need to parse the values by the underscore.

0

1 Answer 1

6

Python's split() returns a list object (an array), so if you use split("_") on "A_B_C_D" you get an array where you can access every Letter by its index. The underscore acts as an separation character in this case, and parsing character separated lists is one of the most common uses of the split() function

fielda=

field1.split("_")[0]

fieldb=

field1.split("_")[1]

fieldc=

field1.split("_")[2]

fieldd=

field1.split("_")[3]

Appendix: The index [-1] means counting the index of the array backwards. So '-1' refers to the last element in the list, '-2' to the second last, '-3' to the third last and so on.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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