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 Answers 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– BillTheCat2016年02月05日 14:27:54 +00:00Commented Feb 5, 2016 at 14:27
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– BillTheCat2016年02月05日 03:28:53 +00:00Commented Feb 5, 2016 at 3:28
-
1String formatting requires single-quotes:
"COUNTY = '{:s}'".format(cname)
Vince– Vince2016年02月05日 03:40:11 +00:00Commented 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– Erica2016年02月05日 09:10:46 +00:00Commented Feb 5, 2016 at 9:10
"COUNTY "
rather than"COUNTY"
(space before the double-quote mark) in yourwith
line