I have two columns in a personal geodatabase table that I'm trying to add up (to a third field) using a python function in field calculator, however some of the string values are not real numbers.


field1     field2
12.24      2
h3.3       4
xyz        1
932.33.32  3

I have a conditional statement that checks for letters, however I'm stuck on how to check for the two decimal character occurrence,


For the two decimal string example I just want to return the value of field2. Here is my code so far.

import re
def cal(field1, field2):
  #check for two decimal character occurrence
  elif re.search('[0-9]+', x):
    myvalue = float(field1) + float(field2)
    return myvalue
    myvaluefinal = float(field1) + float(field2)
    return myvaluefinal

2 Answers 2


Given your use case, you could test for a "two decimal string" like this:

str_test = '123.456.789'
if len(str_test.split('.')[0::1]) == 3:
    print 'This is a string which contains two decimals... do something'

You would of course want to build in some more elaborate type and formatting logic, but this is the general idea.

  • Thanks, your code does work, however I could not figure out how to include it into my function. I posted another way to do it.
    – artwork21
    Commented Jul 30, 2012 at 14:12

I figured out another way to do it, see code below:

def cal(x, x2):
  def isfloat(str):
    try: float(x)
    except ValueError: return False
    return True
  checkfloat = isfloat(x)
  if checkfloat == False:
    return x2
  elif checkfloat == True:
    myvaluefinal = float(x) + float(x2)
    return myvaluefinal
  • 1
    Is there any way the re.search part of this code will ever be executed?
    – whuber
    Commented Jul 30, 2012 at 15:13

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.