2

I'm using a Python script with ArcGIS 10 to count the number of unique string values in a feature class. My script works if there are values in the field but if there are any NUll values in the field it crashes. Any Ideas?

My code is below:

import arcpy
import operator
inTable = arcpy.GetParameterAsText(0)
stringField = arcpy.GetParameterAsText(1)
stringList = [row.getValue(stringField) for row in arcpy.SearchCursor
 (inTable, "", "",stringField)]
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Feb 2, 2012 at 14:45
2
  • 1
    Try using the SearchCursor command with no parameters besides inTable: stringList = [row.getValue(stringField) for row in arcpy.SearchCursor(inTable)] Commented Feb 2, 2012 at 14:49
  • Does the script fail during the loop through the SearchCursor or does it fail later on? It seems that the list comprehension should be able to handle collecting None values, which is the Python representation of NULL. Commented Feb 27, 2012 at 14:24

2 Answers 2

4

Null values are equal to the python None so you could also handle null values like this

if row.getValue(stringField) == None:
 do something
if row.getValue(stringField) != None:
 do something else
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
answered Feb 27, 2012 at 9:38
2

Use isNull(field_name) method before getting values. For further info see this help topic

answered Feb 2, 2012 at 15:29
1
  • Do you mean I need to count the null values seperately? Commented Feb 2, 2012 at 16:24

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.