I am fighting a bit with the cursor function since this morning. Thanks to the help of the people here and some tutorials I found I got this far
import arcpy
arcpy.env.workspace = "C:\\Users\\Documents\\Scoring\\Ecodis_clip_split" #set workspace to folder with values
value = [] #create list for the returning value of searchCursor
eco = arcpy.SearchCursor ("Anagance.shp", "", "", "area_ha", "")
#search for the value in attributetable
for row in eco:
value.append(row) #write the value into the list
arcpy.env.workspace = "C:\\Users\\Documents\\Scoring\\Wetland"
#set workspace to folder with values
diss = arcpy.UpdateCursor ("diss_1.shp", "", "", "area_eco", "")
for row in diss:
row.setValue(area_eco, row.getValue(value)) #identify all the rows and Update die field area_eco with the extracted value from the search cursor out of the list
cursor.updateRow(row)
del eco
del diss
It bounces back with
area_eco is not defined
Which means - if I understood the concept of python correct - that this is not a variable. Ture, its my field which needs to updated. What am I doing wrong?
1 Answer 1
If there is only 1 value in your intial table, this will work, but if there are more, other code will be required.
import arcpy
arcpy.env.workspace = "C:\\Users\\Documents\\Scoring\\Ecodis_clip_split"
#set workspace to folder with values
# YOU DON'T need to do this for 1 value -- value = [] #create list for the returning value of searchCursor
## IF YOU HAVE MULTIPLE FIELDS - then you need to put them in a list or dictionary, and can reference them in the UpdateCursor.
eco = arcpy.SearchCursor ("Anagance.shp")
# loop through - this will assign the last AREA_ECO row value to the variable 'value' as you said you wanted.
for row in eco:
value = row.AREA_ECO
arcpy.env.workspace = "C:\\Users\\Documents\\Scoring\\Wetland"
diss = arcpy.UpdateCursor ("diss_1.shp", "", "", "area_eco", "")
for row in diss:
row.setValue("AREA_ECO", value)
diss.updateRow(row)
del eco
del diss
-
That works. I had to change that, I had to change the first
value=row.AREA_ECO
to...row.area_ha
. I have some questions regarding the code. I want to understand it that I do not have to come here the next time and ask 1000 questions. Can I shorten thatdiss = arcpy.UpdateCursor ("diss_1.shp", "", "", "area_eco", "")
todiss = arcpy.UpdateCursor ("diss_1.shp")
? And thisdiss.updateRow(row)
is a thing that has to be written with the updateCursor? Otherwise it does not update it, even if we are setting the value here already?row.setValue("area_eco", value)
four-eyes– four-eyes2014年06月27日 18:20:06 +00:00Commented Jun 27, 2014 at 18:20 -
Also, if you know the value of "AREA_ECO" you can hard code it in, and skip the first search cursor.dklassen– dklassen2014年06月27日 18:20:15 +00:00Commented Jun 27, 2014 at 18:20
-
correct - you can shorten that statement if you want, and it will work fine. The diss.updateRow(row) - this will update the row. You set the name of your cursor to be 'diss' that is why it looks like that, you can set it to xyz and it would then read... xyz.updateRow(row)dklassen– dklassen2014年06月27日 18:22:03 +00:00Commented Jun 27, 2014 at 18:22
-
ok. thanks. next step will be populate the field with different variables from different rows. but that will be next week :) Thanks for your helpfour-eyes– four-eyes2014年06月27日 18:32:21 +00:00Commented Jun 27, 2014 at 18:32
-
to do that you need to have a corresponding field in the 2 feature classes.dklassen– dklassen2014年06月27日 18:33:44 +00:00Commented Jun 27, 2014 at 18:33
value.append(row)
in your firstfor
loop.row.setValue("area_eco", row.getValue(value[1]))
to access the first index. But it sayslistindex out of range
.