I have several files (shapefiles). Table 1 has only one row with several fields whereas Table 2 has several rows. What I want is to take a value from Table 1 and populate all rows in Table 2. I used a SearchCursor to find the value I want and an UpdateCursor to populate the rows.
import arcpy
arcpy.env.workspace = "C:\\Users\\Documents\\Scoring\\Ecodis_clip_split"
#set workspace to folder with values
rows = arcpy.SearchCursor ("Table1.shp", "", "", "area_ha", "")
#search for the value in attributetable
arcpy.env.workspace = "C:\\Users\\Documents\\Scoring\\Split_wetlandt"
#set workspace to folder with files which need to be updated
rows = UpdateCursor ("Table2.shp", "", "", "area_eco", "")
#Update all rows of file with value from SearchCursor above
Running this, I keep getting this Errormessage:
Traceback (most recent call last):
File "<module1>", line 17, in <module>
NameError: name 'UpdateCursor' is not defined
If something is not defined, this means that Python sees it as a variable? Even if I could get rid of the errormessage: would it populate all the rows in Table 2 with the value or do I need to do something else? Note that I just started to learn Python and never scripted anything what so ever...
Edit 6/17/14 11:32 am
@ Aaron, would this replace the values in Table 2 with my searched values from table 1?
import arcpy
arcpy.env.workspace = "C:\\Users\\Documents\\Scoring\\Ecodis_clip_split"
#set workspace to folder with values
rows = arcpy.SearchCursor (table 1", "", "", "area_ha", "")
#search for the value in attributetable
arcpy.env.workspace = "C:\\Users\\Documents\\Scoring\\Split_wetlandt"
#set workspace to folder with files which need to be updated
with arcpy.UpdateCursor (Table 2 ["area_eco"])
#Update all rows of file with value from SearchCursor above
for row in cursor:
area_eco.replace(SearchCursor)
EDIT 6/27614 12:04pm
I updated my script to this. Somehow it deleted my whole attributetable in Table 2 but I got it restored. I run it again and now nothing happens. It does not update the rows, the value in the rows stays 0.
import arcpy
arcpy.env.workspace = "C:\\Users\\Documents\\Scoring\\Ecodis_clip_split"
#set workspace to folder with values
rows = arcpy.SearchCursor ("Table1", "", "", "area_ha", "")
#search for the value in attributetable
arcpy.env.workspace = "C:\\Users\\Documents\\Scoring\\Wetland"
#set workspace to folder with files which need to be updated
rows = arcpy.UpdateCursor ("Table2", "", "", "area_ha", "")
del rows
-
1You forgot arcpy.UpdateCursor()Paul– Paul2014年06月27日 14:23:20 +00:00Commented Jun 27, 2014 at 14:23
3 Answers 3
You forgot to specify the arcpy module when calling UpdateCursor.
rows = arcpy.UpdateCursor ("Table2.shp", "", "", "area_eco", "")
I second Aaron regarding using the with statement
Also, I would reference one of my OS answer regarding using either the "classic" cursor or the data access (da) one.
Finally, I would like to point you to a few resources to help you learn python/arcpy:
Hope it helps.
-
Updating this an trying to run the script, it bounces back with this
RuntimeError: ERROR 999999: Error executing function. Cannot acquire a lock. Cannot acquire a lock.
four-eyes– four-eyes2014年06月27日 14:41:14 +00:00Commented Jun 27, 2014 at 14:41 -
Close ArcMap/ArcCatalog and other Python IDEs that you have open.ericchiasson– ericchiasson2014年06月27日 14:43:54 +00:00Commented Jun 27, 2014 at 14:43
-
Weeeell, something happend, but not what I wanted it to do. It deleted now my whole attributestable instead of updating it...four-eyes– four-eyes2014年06月27日 14:49:19 +00:00Commented Jun 27, 2014 at 14:49
-
@Stophface I hope you have a backup...ericchiasson– ericchiasson2014年06月27日 14:57:16 +00:00Commented Jun 27, 2014 at 14:57
-
Sure I do :) Got it restored already. But where is the problem?!four-eyes– four-eyes2014年06月27日 14:59:16 +00:00Commented Jun 27, 2014 at 14:59
What is going on here is that the Python interpreter does not know where "UpdateCursor" came from. The correct syntax is as follows (i.e. ArcGIS 10.1+):
arcpy.da.UpdateCursor(YourFeatureclass, ["Field1", "Field2", "Field3", "etc"])
A very clean way to implement a cursor is by using the with
statement:
with arcpy.da.UpdateCursor(YourFeatureclass, ["Field1", "Field2", "Field3", "etc"]) as cursor:
for row in cursor:
# Do something
Cursor objects are automatically opened, processed in some way and closed automatically using a with
statement. More details are available on the ESRI help section on UpdateCursors.
-
Hey. Thank you! I edited my post. I am not sure how I tell 'do something' to use the value of my search cursorfour-eyes– four-eyes2014年06月27日 14:36:10 +00:00Commented Jun 27, 2014 at 14:36
-
1@Stophface I would recommend opening a new question to address that. My initial thought is to run a search cursor and populate a list and then use that list to update a the new table via the update cursor.2014年06月27日 14:45:19 +00:00Commented Jun 27, 2014 at 14:45
-
Ah, alright. Well, I tried the script I wrote with the added
arcpy
and it deleted my attributetable the shapefile I wanted to update.four-eyes– four-eyes2014年06月27日 14:52:40 +00:00Commented Jun 27, 2014 at 14:52
This is based on my understanding. I didn't test the script but this should work. Provide me a link to your data if you need further debugging.
import arcpy
arcpy.env.workspace = "C:\\Users\\Documents\\Scoring\\Ecodis_clip_split"
#set workspace to folder with values
# Create a cursor for Ecodis
cursorEco = arcpy.SearchCursor ("Table1", "", "", "area_ha", "")
#search for the value in attributetable
arcpy.env.workspace = "C:\\Users\\Documents\\Scoring\\Wetland"
#set workspace to folder with files which need to be updated
# Create a cursor for Wetland
cursorWet = arcpy.UpdateCursor ("Table2", "", "", "area_ha", "")
# Itereate each row within the Ecodis cursor
for rowEco in cursorEco:
print(rowEco.getValue("area_ha"))
# Itereate each row within the Wetland cursor
for rowWet in cursorWet:
# Assign the value of Ecodis row to the Wetland row
rowWet.setValue("area_ha", rowEco.getValue("area_ha"))
# Save the changed made to the Weltand
cursorWet.updateRow(rowWet)
# Clean-up
if cursorEco:
del cursorEco
if cursorWet:
del cursorWet
if rowEco:
del rowEco
if rowWet:
del rowWet
-
Hey thanks a lot for your effort.
SyntaxError: Cant assign to function call (<module3>, line 34)
pops up highlighting this bit of coderowWet.setValue("area_ha") = rowEco.getValue("area_ha")
I guess there is a mistake in the Syntax? I put already 4 spaces in there but that did not help.four-eyes– four-eyes2014年06月27日 17:31:04 +00:00Commented Jun 27, 2014 at 17:31 -
@Stophface I've made a change. See how it works.ericchiasson– ericchiasson2014年06月27日 17:41:12 +00:00Commented Jun 27, 2014 at 17:41
-
It would likely be more efficient if you accessed the value of row.Eco only once. Also, you can use a try...except block for your deletions. Less lines and it handles exceptions with lock files better.Paul– Paul2014年06月27日 17:45:12 +00:00Commented Jun 27, 2014 at 17:45
-
Error executing function. The index passed was not within the valid range
. Aaaand it delets all my rows in the attributetable where the value is supposed to go in again.four-eyes– four-eyes2014年06月27日 18:11:30 +00:00Commented Jun 27, 2014 at 18:11 -
Hey. I tried something different with a list and had some questions about that. The solution is here gis.stackexchange.com/questions/103515/… Thanks a lot for your effort :)four-eyes– four-eyes2014年06月27日 18:21:46 +00:00Commented Jun 27, 2014 at 18:21