[GIS] how to update values from python list into feature class

arcgis-10.2arcpyattribute-tablepython-2.7update

I have a feature class and python list. Feature class has 80 fields and 500 records. List also has 80 sub-list and 500 elements in each. So basically they are same dimension so to say.

I want to copy all records in python sub-list to corresponding fields in feature class. I was refering to an earlier post:

How to update attributes of a shapefile using list values?

I tried the code below but it doesnot work. there is some logic error can anyone correct me.

Code:

    fc = parameters[0].valueAsText   # feature class selected from python toolbox 

    fname = parameters[1].valueAsText   #excel workbook selected from python toolbox. 


    xl_workbook = xlrd.open_workbook(fname)
    sheet = xl_workbook.sheet_by_index(0)

    #here i am copying the excel sheet data to python list.
    l = [[sheet.cell_value(r,c) for r in range(sheet.nrows)] for c in range(sheet.ncols)]

    #sample of list : 
    # l = [[u'OBJECTID', 1.0 , 2.0 , 3.0 , 4.0 ], [u'LENGTH', 56.29, 61.8 , 11.01 ,164.03]]

    #till here everything is working fine. 
    #now i want to update the fields in feature class (fc) with the python list (l)


    i = 0
    cursor = arcpy.UpdateCursor(fc)
    for row in cursor:          
        row[i] = l[i]
        cursor.updateRow(row)  #here i get 'valueError: Row: Invalid value for setting'
        i = i+1

Best Answer

I have tried to understand what you are asking without success.

What I can say is that I think your code snippet should start from code like below:

l = [[u'OBJECTID', 1.0 , 2.0 , 3.0 , 4.0 ], [u'LENGTH', 56.29, 61.8 , 11.01 ,164.03]]

print l[0]

From this you can see that a list gets printed:

[u'OBJECTID', 1.0, 2.0, 3.0, 4.0]

However, I am unable to see the schema that you are trying to write this into using arcpy.UpdateCursor(), and personally I think you will be better using arcpy.da.UpdateCursor(), because the code you are using looks more like it may be derived from a new style cursor.