HowTo:  Insert unique values into table

相关信息
Article ID: 41137
Software:
ArcSDE 10.1
ArcGIS for Desktop Advanced 10.1
ArcGIS for Desktop Standard 10.1
ArcGIS for Desktop Basic 10.1
Platforms:
Windows Server 2003, Vista, Windows 7, Windows 8

问题描述
GIS administrators or programmers sometimes need to insert unique values into a column in a feature class or table. To do this, a method is needed to check values that are to be inserted against existing values in the table. The below Python script example provides such a method for inserting unique values in a specified column while creating point feature classes.
已邀请:

易智瑞技术支持

赞同来自:

解决方案
Modify the below parameters in the script:


#path to table via database connection file
input_table = r"C:\connections\lclinton2\sde101@ lclinton2.sde\sde101.SDE.t2"

#Column or Field that needs unique values
unique_field = "U_ID_IDX"

#A list of values that will be used to construct new rows (There are four inserts shown below).
#For each insert, the first is the unique value (12) and X,Y coordinates such as (1,2).
row_vals =[((12),(1,2)),((23),(2,10)),((21),(3,2)),((26),(3,2))]




import arcpy, os, string

def InsertUnique(input_table,unique_field,row_vals):
#create python list
check_vals = []
rv_list = []
#append existing values
rows = arcpy.SearchCursor(input_table,"","",unique_field)
for row in rows:
check_vals.append(row.getValue(unique_field))
del row

for rv in row_vals:
rv_list.append(rv)

#check field for existing vals and compare to row_vals
for r in rv_list:
if r[0] in check_vals:
row_vals.remove(r)
print "NOT inserting non-unique value {0}".format(r)
else:
print "Not in list, Preparing to insert unique value"
del r


# Open an InsertCursor
cursor = arcpy.da.InsertCursor(input_table, (unique_field,"SHAPE@XY"))

for row in row_vals:
cursor.insertRow(row)
print "Inserting unique values {0}".format(row[0])

#Delete cursor object
del cursor, check_vals, row_vals, rows, rv_list

if __name__ == "__main__":

#table and field variables
input_table = r"C:\connections\lclinton2\sde101@ lclinton2.sde\sde101.SDE.t2"
unique_field = "U_ID_IDX"
# A list of values that will be used to construct new rows
row_vals = [((12),(1,2)),((23),(2,10)),((21),(3,2)),((26),(3,2))]
InsertUnique(input_table,unique_field,row_vals)



创建及修改时间
Created: 5/2/2013

Last Modified: 8/6/2013
原文链接
http://support.esri.com/en/kno ... 41137

要回复问题请先登录注册