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.

  • looks like "COUNTY " rather than "COUNTY" (space before the double-quote mark) in your with line – Midavalo Feb 5 '16 at 2:06
  • For debugging purposes you can wrap the with clause with a try..except in order to see what happens. – Stefan Feb 5 '16 at 10:17
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:
  • Finally got my whereclause to work! Thanks. More study time on formatting is a must. – BillTheCat Feb 5 '16 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.

  • Thanks for the formatting suggestions and example. I'm still getting the same error. – BillTheCat Feb 5 '16 at 3:28
  • 1
    String formatting requires single-quotes: "COUNTY = '{:s}'".format(cname) – Vince Feb 5 '16 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 Feb 5 '16 at 9:10

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.