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.

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Feb 5, 2016 at 1:52
2
  • looks like "COUNTY " rather than "COUNTY" (space before the double-quote mark) in your with line 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. 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:
answered Feb 5, 2016 at 4:27
1
  • Finally got my whereclause to work! Thanks. More study time on formatting is a must. 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.

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

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.