InsertCursor will not work with the Set function

325
8
Jump to solution
03-16-2018 01:07 PM
LarryAdgate
Occasional Contributor

#I would really appreciate some help on this: I need to use the Set function on this script to remove all
# my duplicates but the InsertCursor refuses to work here. Any Ideas, Thank You- Larry Adgate

import arcpy

fc1 = "Database Connections\\TaskmasterServer.sde\\sde_gsw.GSW_SDE.CCB_Updates1"
fc2= "Database Connections\\TaskmasterServer.sde\\sde_gsw.GSW_SDE.CCB_Data\\sde_gsw.GSW_SDE.Master_CCB_Points"
fc3 = "K:\\808 - TariffMaps\\MyResults\\MyTable.dbf"

set_one = set(r[0] for r in arcpy.da.SearchCursor(fc1,"PREM_ID"))
set_two = set(r[0] for r in arcpy.da.SearchCursor(fc2,"PREM_ID"))
print "Items unique to the Primary Field: {}".format(", ".join(sorted(set_one-set_two)))

results = set(set_one-set_two)
print results


cursor = arcpy.da.InsertCursor(fc3, ["result"])
#Below is my error= TypeError: value #0 - unsupported type: set
cursor.insertRow([results])

del cursor

0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

What about:

cursor = arcpy.da.InsertCursor(fc3, ["result"])
for i in results:
    cursor.insertRow([i])

del cursor

View solution in original post

8 Replies
JoshuaBixby
MVP Esteemed Contributor

In the geodatabase, what data type is the "result" field in for fc3?  You can't store Python data structures likes lists, tuples, sets, dictionaries, etc... directly in a record in a geodatabase, so I am trying to understand what data type "result" is since the field is already created.

LarryAdgate
Occasional Contributor

Hi Joshuba,

My result data structure are numbers in a text field: Ex: 0000256677

My numbers have leading zeros so the field needs to be text 

Thank You,

Larry

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

what does one of your sets look like?  Something like set(000025667, 000026443, ....)?  If so, how do you want that to look in the text field in the geodatabase?

0 Kudos
XanderBakker
Esri Esteemed Contributor

You can cast the result to string before writing the result using something similar as you already did with:

", ".join(sorted(set_one-set_two))

(this may generate too many characters to be contained in the field), but I don't understand the purpose of what you are trying to do. Every record will have a string representing the list of ID's that are in fc1 but not in fc2. When you have that, what will you do with this result?

LarryAdgate
Occasional Contributor

A A Typical set is in a text Field column. called PREM_ID-

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

What about:

cursor = arcpy.da.InsertCursor(fc3, ["result"])
for i in results:
    cursor.insertRow([i])

del cursor
LarryAdgate
Occasional Contributor

Thank You so Much........it Works

Larry

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

You inadvertently marked your answer as correct, I unmarked it, please go back and mark the correct response as being the one

0 Kudos