Tell me more ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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? Any comments would be appreciated. Thanks in advance.

share|improve this question
2  
Is the !fieldx! notation something specific to arcgis? In standard Python, that would be the syntax error. – Thomas K Jan 26 '12 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 Jan 26 '12 at 12:47

migrated from stackoverflow.com Jan 29 '12 at 7:00

1 Answer

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.

share|improve this answer
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. – user3011 Jan 27 '12 at 20:11
Fixed, will now handle ints and friends. – Jason Scheirer Feb 28 '12 at 8:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.