1

I have a feature class with a big number of features.

In the attribute table I have a string field made from two numbers separated by an underline (e.g.: 0_1, 98_237, 31_31...). I want to create two new numerical (long integer) fields with the numbers before and after the underline (e.g.: 0_1 0 1, 98_237 98 237, 31_31 31 31...)

How can I do this using field calculator?

2

within the filed calculator, you can use python statement with the split() function

with the first number, it will be

int(!field!.split("_")[0]) #creates a list of items separated by the _, and take the first item

and for the second number

int(!field!.split("_")[-1]) #creates a list of items separated by the _, and take the last item (if you have only 2 items, you could also use 1(= the second item in Python index)

int() is not necessary if you have an integer field as output, but I prefer to use it so that the conversion is obvious. If your number are floating points, use "float()" instead of "int()"

  • Thanks. But it didn't worked here. I put exactly: int(!ID_APR!.split()[0]); ID_APR is the field I want to split. Got the error 000539: Error running expression: int (u "0_2".split()[0]). The complete error message: Messages Executing: CalculateField RJ_Linhas300_Protegidas FID1 int(!ID_APR!.split()[0]) PYTHON_9.3 # Start Time: Fri Jan 04 10:17:02 2019 ERROR 000539: Error running expression: int(u"0_2".split()[0]) Traceback (most recent call last): File "<expression>", line 1, in <module> ValueError: invalid literal for int() with base 10: '0_2' Failed to execute (CalculateField). – Felipe Gonçalves Jan 4 at 12:21
  • Sorry, I forgot to type the _ on which you want to split. by default it is split by a space – radouxju Jan 4 at 12:28

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.