Saturday 19 December 2015

arcpy - Sum fields from ListFields - RuntimeError: A column was specified that does not exist?


Does anyone have a suggestion on how to otherwise solve the problem ... The need to automatically generate all the columns from the table, the calculation of the sum, the results are updated in the new column? Recognizing the conditions: 1) did not know the names of columns 2) the name of the column to update the known example table:


enter image description here


PS I am an absolute beginner in python programming


 import arcpy, os, string
fc = "D:\\path\\test.shp"




fieldNameList = []
fields = arcpy.ListFields(fc, "*")

for field in fields:
if field.type in ("Double", "Integer", "Single", "SmallInteger"):

fieldNameList.append(field.name)
fn = "'" + "', '".join(fieldNameList) + "'"
print(fn)
with arcpy.da.UpdateCursor(fc, [fn]) as cursor:


for row in cursor:

row[4] = row[0] + row[1] + row[2] + row[3]
cursor.updateRow(row)

ERROR - RESULT


 >>> 
'tt', 'ff', 'vv', 'rr', 'update'


Traceback (most recent call last):
File "E:\path\test.py", line 17, in
for row in cursor:
RuntimeError: A column was specified that does not exist.
>>>

Answer



You are currently passing a single string, so the cursor is looking for a single field named 'tt', 'ff', 'vv', 'rr', 'update' instead of individual fields tt, ff, vv (etc.)


Your field name list can be directly used in the UpdateCursor:


with arcpy.da.UpdateCursor(fc, fieldNameList) as cursor:

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...