I have a list of values which I am trying to add to an existing shapefile. The field where the values need to be inserted has been added via arcpy.AddField_management and thus the values of the rows are empty or 0. The variable expression1 is a list of floats with the length of the rows in the shapefile. To update the rows I use UpdateCursor:
import arcpy import math from arcpy import env env.workspace = "S:/Black grouse modelling/shapefiles" inFishnet = "fishnet_label.shp" rows = arcpy.UpdateCursor(inFishnet) fields = arcpy.ListFields(inFishnet) for row, i in zip(rows, expression1): for field in fields: if field.name == "dist": print i row.setValue(field.name, i) rows.updateRow(row) del rows del row
The output I get is as follows:
1265.60109093 Traceback (most recent call last): File "<pyshell#218>", line 6, in <module> rows.updateRow(row) File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\arcobjects\arcobjects.py", line 102, in updateRow return convertArcObjectToPythonObject(self._arc_object.UpdateRow(*gp_fixargs(args))) RuntimeError: ERROR 999999: Error executing function.
The value I want it to update with gets printed as requested (1265.60109093). And I think there isn't a problem with the row.setValue() line. The error comes when I am trying to update the row. I have thought it might be an issue with the float numbers. As these might be longer than the field allows for. However, I have tried rounding the numbers and then inputting those values into the rows but that doesn't work either. I have also tried the following code without setValue() but this doesn't work either:
for row, i in zip(rows, expression1): print i row.dist = i rows.updateRow(row)
Any help is appreciated.
as requested the values of expression1:
expression1[:5] [1265.6010909259928, 1247.9398102685327, 1232.0557693355686, 1218.0185004559901, 1205.8924977596705]
which is calculated via the following function:
def distFrom(self, other): ox = other.x oy = other.y xDist = self.x - ox yDist = self.y - oy return (xDist**2 + yDist**2)**0.5
Obviously I do not need these values to be so accurate, but rounding them to 2 decimals for example does not solve the problem.
I have also tried it with a different variable where the values are either True or False and converting these to strings. This does also not solve the problem.
I am using python 2.6.5 and arcGIS 10.0