Query to have only numeric values in the field

8120
6
05-17-2016 06:38 AM
LianaSmith
Occasional Contributor II

I have a feature class and one of its fields is Route type. I need to exclude from this Feature class all Routes that have any letter in the value (my values are 1203,1234,123B, BB and etc) I should have only numeric ones.

How can I do it in my Python code?

Thank you in advance!!!!

0 Kudos
6 Replies
DanPatterson_Retired
MVP Emeritus

There are options but this will get you thinking.  The results return a string which you can convert to an integer.

Since you have not indicated that you want decimals returned, this example is for integers.  Now to test for all are numbers, you don't parse the string you just check to see if they are all numbers as in the second example

>>> a   # parse a string returning only the numbers
'123456AB78C'
b = "".join([i for i in a if i.isdigit()])
>>> b
'12345678'
>>>
>>> a    #  check to see if all are numbers
'123456AB78C'
>>> b = a.isdigit()
>>> b
False
>>> 
LianaSmith
Occasional Contributor II

Dan,

I am new to Python, so I am bit confused what exactly I need to put in python. How I specify which feature class I want to edit and which field? I do not need decimals, just integers

This code will be a part of a batch job so there will be other feature classes as well.

Thank you in advance for your help!

0 Kudos
DanPatterson_Retired
MVP Emeritus

Ok... so do you know how to

A little much and you are already jumping to batches.

What I might suggest for the interim for checking is the following

  • add a field to the feature classes table, called test, make it an integer field
  • using the field calculator, calculate values for that new field using the following idea
  • use the python parser, and calculate    int( !YourFieldWithClasses!.isdigit())
  • this will produce a field of 0 or 1 as in the example below

>>> a = "123456bb345"

>>> int(a.isdigit())

0

>>> a = "123456"

>>> int(a.isdigit())

1

Now if you just manually query the field for 1, then they are all the good fields and you can export them to a new feature class.

If you like that, it will save a bunch of stuff that you probably don't want to do now if you are learning.  If it works and it seems easy, then the next stage is coding.

0 Kudos
LianaSmith
Occasional Contributor II

Can I do it simpler using Field Calculator and writing small code there?

0 Kudos
DanPatterson_Retired
MVP Emeritus

the part where I said add the field and ...

  • use the python parser, and calculate    int( !YourFieldWithClasses!.isdigit())

yes

Not the part about cycling through featureclasses, adding the field and doing the calculation manually... that is the scripting part

Which is why I said try it manually and if you like it then do some reading and it can be scripted...with help probably

RichardFairhurst
MVP Honored Contributor

if the data source is a file geodatabase you can use Select By Attribute with an SQL expression like:

CAST(ROUTE_TYPE AS FLOAT) > -10000000

Unfortunately this does not work with shapefiles, and I could not find help showing a similar method for that data source.  But I highly recommend that everyone convert their shapefiles to a file geodatabase for its superior performance and many other enhancements.  SDE data source types may have alternative methods of casting.