Select to view content in your preferred language

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

4193
1
Jump to solution
03-06-2015 05:28 AM
DavorinBajic1
Deactivated User

Does someone can help?

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 <module>
  
for row in cursor:
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?

thanks in advance

PS I am an absolute beginner in python programming

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
DavorinBajic1
Deactivated User

I solved the problem...

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)   

     with arcpy.da.UpdateCursor(fc, fieldNameList) as cursor:  
          for row in cursor:  
               row[4] = row[0] + row[1] + row[2] + row[3] 
          cursor.updateRow(row)

View solution in original post

0 Kudos
1 Reply
DavorinBajic1
Deactivated User

I solved the problem...

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)   

     with arcpy.da.UpdateCursor(fc, fieldNameList) as cursor:  
          for row in cursor:  
               row[4] = row[0] + row[1] + row[2] + row[3] 
          cursor.updateRow(row)
0 Kudos