0

ArcGIS keeps throwing an error when I use the following script in the field calculator, using Python:

pre-logic script code:

y= ()
if !fieldx!.isalnum():
    y= 1
else:
    y= 0

Fieldy=

y

The error in the results window reads:

'ERROR 999999: Error executing function, Syntax Error, Failed to execture (CalculateField)'

I've checked the classes of the fields: fieldx is a string and fieldy a short integer, so that can't be the problem. I've also enabled the shapefile for editing. Note that I've added fieldy using the addfield function.

Other fieldcalculations, for example:

fieldy = 5

do work, so the problem should be in the syntax.

What am I doing wrong?

2
  • 2
    Is the !fieldx! notation something specific to arcgis? In standard Python, that would be the syntax error.
    – Thomas K
    Commented Jan 26, 2012 at 12:41
  • Hi Thomas, thanks for your consideration. Yes, I believe that's specific for ArcGIS. When I insert the fieldname by clicking the field from the field list, this is how it appears in the code. ArcGIS help docs also use this notation in their Python examples.
    – HDR
    Commented Jan 26, 2012 at 12:47

1 Answer 1

3

That needs to be treated as a function.

In your code block:

def AlnumField(field_value):
   if str(field_value).isalnum():
     return 1
   else:
     return 0

Then in your expression: AlnumField(!fieldx!)

The !fieldname! notation is only respected in the expression, not the code block.

2
  • In python the .isalnum() method is a string method. I'm not sure this will work if an int is provided as the field_value.
    – garnertb
    Commented Jan 27, 2012 at 20:11
  • Fixed, will now handle ints and friends. Commented Feb 28, 2012 at 8:41

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.