3

I'm working on this script which creates a table within a geodatabase. It then grabs the unique values from a list and attempts to insert them into the table using an Insert Cursor.

I get the Error

Runtime error
Traceback (most recent call last):
File "<string>", line 25, in <module>
TypeError: sequence size must match size of the row

My code is as follows:

import arcpy
from arcpy import env
env.workspace = "C:\Users\USER\Desktop\BaseLayer\UniqueValue.gdb"

## Designate the input layer.
featureclass = "Q:\Admin\IP_AnnoTools\Intermediates\Intersects\Output_Hydro_Line_Intersect.shp"## Parameter
field = "FEATURE_TY"## Parameter
valueList = []
rows = arcpy.SearchCursor(featureclass)
for row in rows:
    valueList.append(row.getValue(field))

uniqueSet = set(valueList)
uniqueList = list(uniqueSet)
uniqueList.sort()


arcpy.CreateTable_management("C:\Users\USER\Desktop\BaseLayer\UniqueValue.gdb", "unique")
arcpy.AddField_management("C:\Users\USER\Desktop\BaseLayer\UniqueValue.gdb\unique","UNIQUE_VAL","TEXT","","","100")
unique = "C:\Users\USER\Desktop\BaseLayer\UniqueValue.gdb\unique"

cursor = arcpy.da.InsertCursor(unique,("UNIQUE_VAL"))

for uv in uniqueList:
    cursor.insertRow(uv)

del cursor
print uniqueList
del rows
del row

My output when I print my list is:

[u'FLOW-ARB-MANUAL', u'LAKE-REP-PRI', u'RIV-MAJ-REP-PRI', u'STR-INDEF', u'STR-PER', u'STR-RECUR']
0

2 Answers 2

11

You have to insert a list, you're inserting a single value.

for uv in uniqueList:
    cursor.insertRow(uv)

should be

for uv in uniqueList:
    cursor.insertRow([uv])
0
5

You are trying to put a string into the insertRow method, but that method expects a list or tuple. The insertrow method first counts the items in the list or tuple to see if the item count matches the row field count, but since a string item count is most likely 0 it always fails to match up with the field count of the row (i.e., 1). So instead try converting each string value into a list with a single string value. That way the count of items in the list will match the field count (1 string item in the list will match 1 string field in the row).

So modify your code to see if this works:

for uv in uniqueList:
    cursor.insertRow([uv])
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.