0

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?

  • you need to have area_eco, typed as "area_eco", although I am not sure what you are doing with the row.getValue(value) portion of your statement, as the value variable is a list. – dklassen Jun 27 '14 at 17:17
  • My intention was to write the value I get from the searchCursor into a list and then write the Value from that list with the updateCursor into my rows. Not good? – Stophface Jun 27 '14 at 17:19
  • You can't do that without accessing the index value; see this StackOverflow question for how to do that. Also, you need to indent value.append(row) in your first for loop. – Erica Jun 27 '14 at 17:33
  • Hello Erica, Thanks a lot for your answere but that StackOverflow Fred is beyond that what I understand. I added this to the Syntax row.setValue("area_eco", row.getValue(value[1])) to access the first index. But it says listindex out of range. – Stophface Jun 27 '14 at 17:52
  • So to summarize what you are trying to do: you have 2 tables, and you are trying to take values from 1 table and populate the 2nd one with them? Correct? if so, what is the linking field - how do you know which "area_eco" to put in each row in the new table? – dklassen Jun 27 '14 at 17:56
2

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 that diss = arcpy.UpdateCursor ("diss_1.shp", "", "", "area_eco", "") to diss = arcpy.UpdateCursor ("diss_1.shp")? And this diss.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) – Stophface Jun 27 '14 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 Jun 27 '14 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 Jun 27 '14 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 help – Stophface Jun 27 '14 at 18:32
  • to do that you need to have a corresponding field in the 2 feature classes. – dklassen Jun 27 '14 at 18:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.