UpdateCursor on two tables with value from SearchCursor

المشرف العام

Administrator
طاقم الإدارة
I try to keep myself busy and learn some python. I have two tables

test2.shp (for searchcursor)



test.shp (needs to be updated)



I want to update test.shp with a UpdateCursor. The column which needs to be updated is column2 if column1 of test.shp satifies a certain criteria (column1 = aa).So, if column1 = aa, insert value in column2.

I try to take the values for the update from test2.shp with a SearchCursor.

That's my code:

import arcpyarcpy.env.workspace = "c:\\Users\\python_ex"scursor = arcpy.SearchCursor("test2.shp", "column1" = 'aa', "", ["column1", "column2"], "")ucursor = arcpy.UpdateCursor("test.shp", "", "", ["column1", "column2"], "")for srow in scursor: if srow == "aa": a = srow.getValue("column2") else: continue for urow in ucursor: if urow == "aa": urow.setValue("column2", a) cursor.updateRow(urow) else: continue del ucursor, urowdel urow, srowI get the error: Keyword cant be an expression where I define the scursor.But I guess that is not my only mistake...

I am working on Arcgis for Desktop 10.2.2Any ideas?

EDIT 8/7/14 12:20pm

Alright,thats what I came up with:

import arcpy#set to folder where features are locatedarcpy.env.workspace = "c:\\Users\\python_ex" #on windows use \\ instead of /#---------------------------#define variables for cursor#---------------------------"""SEARCHCURSOR""""""SearchCursor (in_table, field_names, {where_clause}, {spatial_reference}, {explode_to_points}, {sql_clause})"""#field taken as "identifier". i.e. if column1 == a, take the value from the same row of column2stable = "test2.shp"sfield = ["column1", "column2"]#where clause will be written in SQL and not assigned to variable"""UPDATECURSOR""""""UpdateCursor (in_table, field_names, {where_clause}, {spatial_reference}, {explode_to_points}, {sql_clause})"""#field taken as "identifier". i.e. if column 1 == a, take the value of the SearchCursor and insert into same row, but column2utable = "test.shp"ufield = ["column1", "column2"]#where clause will be written in SQL and not assigned to variable#--------------------------#start the loop#--------------------------with arcpy.da.SearchCursor(stable, sfield, """"column1" = 'aa'""") as scursor: #SQL queries in python are build that way http://resources.arcgis.com/en/help/main/10.2/index.html#//002z0000001r000000 for srow in scursor: print srow with arcpy.da.UpdateCursor(utable, ufield, """"column1" = 'aa'""") as ucursor: #if used with statements, no need to close all the cursors for urow in ucursor: ucursor.updateRow(srow)BUT, that only takes the last the last found value of aa in test2.shp and updates all the other aa in test.shp with this value!



أكثر...
 
أعلى