Select to view content in your preferred language

Field Calculator

10824
14
05-18-2016 06:09 AM
LianaSmith
Frequent Contributor

I need to exclude all values that contain any letter from one of the fields. Basically only integers should be left. What code (Python/VB) do I have to put in field calculator for that?

Thanks!!!

0 Kudos
14 Replies
DanPatterson_Retired
MVP Emeritus

I covered that here Query to have only numeric values in the field

Add a numeric field (integer)

Use the python parser and use the syntax given

int("".join([i for i in !YourFieldName! if i.isdigit()]) )

LianaSmith
Frequent Contributor

Dan, it does not work. That's what I get:

0 Kudos
DanPatterson_Retired
MVP Emeritus

Ohhhh... You failed to mention that some of the entries don't have numbers in them.

So... you have to create a text/string field instead and delete the int( ) part.  Now of course, if there is no numbers in the entry, you will get back an empty string.

The question remains

  • do you want the output to only be numbers?
  • what do you want to do if there are no numbers in the entry?
  • are you good with a text representation of a number? or does it have to be a number?

The rule set will determine the format of the new field, the values it returns and the outputs it will return... especially if you want this as an automagic all encompassing solution.

LianaSmith
Frequent Contributor

Sorry for not mentioning them...I think I can not use field calculator at all.. I dont want to change the values that have letters, I need them to be deleted...

0 Kudos
TedKowal
Honored Contributor

If you want to simply blank out any entry in which GROUTE contains a letter then perhaps you could do this (test on a copy first!):

Since GROUTE could contain letters then it cannot be a number....

GROUTE = "" if any(b.isalpha() for b in !GROUTE!) else !GROUTE!

LianaSmith
Frequent Contributor

Thank you so much Ted! It worked. I assigned empty value for all non-numeric values and then excluded them by querying FC to FC

0 Kudos
LianaSmith
Frequent Contributor

Ted, I exported the field calculator function to Python script and I get error now..

Do you have any idea why? Thanks again

> Error ocurred in:
  File "P:\Batch\J420992\CODE\PYTHON\J420992_C1_Customers.py", line 66, in <module>
    arcpy.CalculateField_management(WCSCUSTOMERCOPY, "GROUTE", " \"\" if any(b.isalpha() for b in !GROUTE!) else !GROUTE!", "PYTHON_9.3", "")

> Error Class is:
<class 'arcgisscripting.ExecuteError'>

> Available error details are:
ERROR 000539: IndentationError: unexpected indent (<expression>, line 1)
Failed to execute (CalculateField).

0 Kudos
DanPatterson_Retired
MVP Emeritus

I new all this sounded familiar!!!

How to remove spaces with Calculate field in ArcMap?

0 Kudos
curtvprice
MVP Esteemed Contributor

You have an extra space:

" \"\" if any(b.isalpha() for b in !GROUTE!) else !GROUTE!"
 ^

I use single quotes to wrap double quotes; easier to read:

'"" if any(b.isalpha() for b in !GROUTE!) else !GROUTE!'
0 Kudos