0

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
  • What happens when you run that code snippet? Are you setting fc to a layer name or a dataset? Can you print fields successfully after setting it? Is there a reason why you are using arcpy.UpdateCursor() instead of arcpy.da.UpdateCursor()? I think you should try to make your code snippet work with say a list of two lists with just three values in each of those because that is something that would be adequately scoped to present here in our focussed Q&A format. Where do you set row and col? Is l a function? If so, where do you define it? – PolyGeo Aug 24 '15 at 8:06
  • When i run the code it gives value Error: invalid input value for setting . .. fc is set to feature class . .. Yes i can print the fields successfully.. there is no particular reason why i am using UpdateCursor, but thats what i know. .. 'l' is the list containing the values which is to be copied. i mentioned here only for reference. that line of code is not part of the actual code. – Jio Aug 24 '15 at 8:24
  • Please use the edit button to improve your question. Potential answerers need to see a code snippet that works up to where you are stuck. Please include the precise error message and the line number of your code snippet that is throwing it. If l is a list show us some test values that we can use for it. Not "80 x 500" just something like "2 x 3". – PolyGeo Aug 24 '15 at 8:30
2

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.

  • There is no benefit of using the old cursors. Aside from being a slightly different syntax, the da tools are superior in every way. – Clay Aug 24 '15 at 13:41
  • @Clay, i agree with you. Could you paste a sample script which does what i am trying to do with arcpy da. basically i am trying to copy each sub-list of a python list to each field of the feature class table. – Jio Aug 25 '15 at 11:53
  • try gis.stackexchange.com/a/74398/53383 – Clay Aug 25 '15 at 15:05
  • @Larry you will need to show us what that list should translate to in the output feature class, and since you are using an update cursor showing what the same rows look like prior to update will also be helpful. I'm assuming you know that ArcPy cursors won't update FID and OBJECTID fields. – PolyGeo Aug 25 '15 at 20:43
  • @PolyGeo , thanks for you help. as of now I converted the list to arcmap table and joined it with feature class. However since i have a huge amount of records the join is taking ages. Do you know if updating fields is faster than join. If so i would like go ahead and dig deeper into this issue otherwise it is ok. – Jio Aug 26 '15 at 7:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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