1

I am trying to select a single entry of a floating point entry in a dbf table that contains 9 entries (sampled from a raster image), some of which is NULL using SearchCursor. I want to select one of the entries that is not NULL and assign it as a new variable for use later in my script. I can do a query that excludes NULL values (includes all values greater than 0) but I don't know how to select a single entry that is not NULL and assign as a new variable.

The field name is:

Rt_b1
<Null>
0.138817
0.136027
0.136027
<Null>
<Null>
0.136027
0.138817
0.136027

Any help would be much appreciated. I'm using Windows XP and ArcGIS 10.1

with arcpy.da.SearchCursor(Sample_TIF2, ("Rt_b1"), '"Rt_b1" > 0') as cursor:
     for row in cursor:
           print str(row[0])
1

First, you probably want to set your field name as a variable so that you can reuse it. Then, build the query string outside the cursor function to use the field name variable.

Once there, unless I'm missing something in your question, it should be a matter of testing for a non-null value in the row. If this comes up true, retrieve the value and set to a variable.

rtfield = "Rt_b1"
querystring = '"' + rtfield + '" > 0'

with arcpy.da.SearchCursor(Sample_TIF2, (rtfield), querystring) as cursor:
  for row in cursor:
    rtblval = row[0]

If this is not your goal, then please clarify your question to more clearly state your need.

  • You could also just perform your null check in the SQL where clause, and just return the first row instead of iterating through all of them. – blah238 Sep 30 '13 at 0:53
  • What you have there looks like exactly what i m am after but I get an error message when I run it. 'tulpe object has no attribute 'getValue' – Simon Sep 30 '13 at 0:53
  • What is the error? – Get Spatial Sep 30 '13 at 0:54
  • Error was 'tuple' object has no attribute 'getValue' – Simon Sep 30 '13 at 0:58
  • 1
    There also shouldn't be any reason to do is not None check because > 0 should never return a NULL value. – Nathan W Sep 30 '13 at 1:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.