3

My goal is to count the number of wells in each county. I've verified the columns are correct as listed in the attribute table. Do I have a typing or formatting error? I wanted to pull county names from a list but I'm trying to T/S with one variable first.

import arcpy
arcpy.overWriteOutput = True
arcpy.env.workspace = "E:\\GIS4080\\Week5\\Lesson5_Data"  

# Variables
wells = "Wells.shp"
counties = "COUNTIES.shp"
Wells_Intersect = "Wells_Intersect.shp"
Wells_Intersect_Layer = "Wells_Intersect_Layer"
input_f = ["Wells.shp", "COUNTIES.shp"]
Date_list = []
wellCnt = 0

# Process: Intersect
arcpy.Intersect_analysis(input_f, Wells_Intersect, "ALL", "", "INPUT")

# Process: Make Feature Layer
arcpy.MakeFeatureLayer_management(Wells_Intersect, Wells_Intersect_Layer)

# List County Names
countyList = [row[0] for row in arcpy.da.SearchCursor(counties, "COUNTY")]
#print countyList

# Count Wells in each County
#for cname in countyList:
cname = "YUMA"
with arcpy.da.SearchCursor(Wells_Intersect_Layer, "COUNTY", '"COUNTY "= ' + cname) as cursor:
    for row in cursor:
        wellCnt = wellCnt + 1
        print cname, str(wellCnt)

Traceback (most recent call last):
File "E:\GIS4080\Week5\L5_hw.py", line 32, in
for row in cursor:
RuntimeError: A column was specified that does not exist.

2
  • looks like "COUNTY " rather than "COUNTY" (space before the double-quote mark) in your with line
    – Midavalo
    Commented Feb 5, 2016 at 2:06
  • For debugging purposes you can wrap the with clause with a try..except in order to see what happens.
    – Stefan
    Commented Feb 5, 2016 at 10:17

2 Answers 2

2

Juggling single quotes and double quotes can be a pain, but this should work:

with arcpy.da.SearchCursor(Wells_Intersect_Layer, "COUNTY", '"COUNTY" = \'{}\''.format(cname)) as cursor:
    for row in cursor:

Although you could probably also get around it by specifying the Where clause separately:

# Count Wells in each County
#for cname in countyList:
countyField = '"COUNTY"'
cname = "YUMA"
whereclause = "{} = '{}'".format(countyField, cname)

with arcpy.da.SearchCursor(Wells_Intersect_Layer, "COUNTY", whereclause) as cursor:
    for row in cursor:
1
  • Finally got my whereclause to work! Thanks. More study time on formatting is a must.
    – BillTheCat
    Commented Feb 5, 2016 at 14:27
2

The problem may be in your where_clause.

'"COUNTY "= ' + cname

You have a space between the attribute name and quotation mark instead of quotation mark and equal sign, which I think will throw it off.

Try instead:

"COUNTY = '{}'".format(cname)
"COUNTY = '" + cname + "'"    # this is the same, but I find it harder to read!

You need those quotation marks there to properly include cname as a string in the SQL query.

3
  • Thanks for the formatting suggestions and example. I'm still getting the same error.
    – BillTheCat
    Commented Feb 5, 2016 at 3:28
  • 1
    String formatting requires single-quotes: "COUNTY = '{:s}'".format(cname)
    – Vince
    Commented Feb 5, 2016 at 3:40
  • @Vince thanks, I always mess that up when I'm going by memory and don't have a Python IDLE at hand.
    – Erica
    Commented Feb 5, 2016 at 9:10

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.