2

I have a shapefile of a river network containing the following fields: toJunction, fromJunction, Int_DAM (which indicates free-flowing status) and Strahler. I'm trying to create a list of the rows where Int_DAM = 'Downstream' using arcpy.da.SearchCursor. I'm getting an error that says:

RuntimeError: A column was specified that does not exist.

Here is the code as I have it:

Sel_list = []
fc = r"C:\Data\Free_Flowing_Rivers_Results\NHN_Canada_Stream_Order_by_Basin_160412.gdb\Datasets\NHN_HN_PrimaryDirectedNLFlow_07_Stream_Order"

fields = ['fromJunction', 'toJunction', 'Int_DAM', 'Strahler']
where_clause = '"Int_DAM"' + "=" + 'Downstream'

with arcpy.da.SearchCursor(fc, (fields), where_clause) as cursor:
    for row in cursor:
        Sel_list.append(row)

print Sel_list       

I've double checked the file and the fields are definitely present. I'm pretty new to Python so I'm sure I'm missing something.

3

It looks like the quote marks in your where_clause are set up wrong.

it should say Int_DAM = 'Downstream' but instead it says "Int_DAM" = Downstream.

Sel_list = []
fc = r"C:\Data\Free_Flowing_Rivers_Results\NHN_Canada_Stream_Order_by_Basin_160412.gdb\Datasets\NHN_HN_PrimaryDirectedNLFlow_07_Stream_Order"

fields = ['fromJunction', 'toJunction', 'Int_DAM', 'Strahler']
where_clause = """ Int_DAM = 'Downstream' """

with arcpy.da.SearchCursor(fc, fields, where_clause) as cursor:
    for row in cursor:
        Sel_list.append(row)

print Sel_list     

Alternatively you can use arcpy.AddFieldDelimiters when constructing your where clause to ensure you've set up the field names correctly. That way you can switch between different types of database. From AddFieldDelimiters - Desktop Help:

The field delimiters used in an SQL expression differ depending on the format of the queried data. For instance, file geodatabases and shapefiles use double quotation marks (" "), personal geodatabases use square brackets ([ ]), and enterprise geodatabases don't use field delimiters. The function can take away the guess work in ensuring that the field delimiters used with your SQL expression are the correct ones.

So your where clause would become:

where_clause = """ {0} = '{1}' """.format(arcpy.AddFieldDelimiters(fc, 'Int_DAM'), 'Downstream')
|improve this answer|||||
1

Copy and past this, it should work

where_clause = "Int_DAM="+"'Downstream'"
|improve this answer|||||
  • should be where_clause - the upper-case W will make this not work as the search cursor refers to where_clause with a lower-case w. Python likes to get picky regarding case. – Midavalo Mar 22 '16 at 17:06
  • really? wouldn't the where_clause just be treated as a variable so in theory it shouldn't matter... – ziggy Mar 22 '16 at 17:09
  • Variable names are case sensitive in python. Where_Clause and where_clause are two different variable names. – Midavalo Mar 22 '16 at 17:19

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.