0

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
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Jun 27, 2014 at 14:16
1
  • 1
    You forgot arcpy.UpdateCursor() Commented Jun 27, 2014 at 14:23

3 Answers 3

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.

answered Jun 27, 2014 at 14:35
8
  • 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. Commented Jun 27, 2014 at 14:41
  • Close ArcMap/ArcCatalog and other Python IDEs that you have open. Commented 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... Commented Jun 27, 2014 at 14:49
  • @Stophface I hope you have a backup... Commented Jun 27, 2014 at 14:57
  • Sure I do :) Got it restored already. But where is the problem?! Commented Jun 27, 2014 at 14:59
1

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.

answered Jun 27, 2014 at 14:22
3
  • Hey. Thank you! I edited my post. I am not sure how I tell 'do something' to use the value of my search cursor Commented 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. Commented 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. Commented Jun 27, 2014 at 14:52
0

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
four-eyes
3,5505 gold badges37 silver badges63 bronze badges
answered Jun 27, 2014 at 17:18
5
  • Hey thanks a lot for your effort. SyntaxError: Cant assign to function call (<module3>, line 34) pops up highlighting this bit of code rowWet.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. Commented Jun 27, 2014 at 17:31
  • @Stophface I've made a change. See how it works. Commented 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. Commented 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. Commented 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 :) Commented Jun 27, 2014 at 18:21

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.