2

I'm trying to update a given field in this case the last one of the table by concatenating all values in the row excepting null values or '' values.

So far when execute the code until line: print (row[ListnamesLeng-1]) i get the concatenation but has been impossible to update the field. I get the message StopIteration: iteration not started. i've tried put cursor.updateRow(row) inside and outside the loop but i keep receiving the same message.

Sorry about my english im still learning! ;)

import arcpy
arcpy.env.overwriteOutput = True


arcpy.PivotTable_management(in_table="C:\Users\daniel.cortes\....", fields="idRegistro", pivot_field="LLAVE_NESTLE_TIENDAS_VENDEDOR", value_field="LLAVE_NESTLE_TIENDAS_VENDEDOR", out_table="C:\Users\daniel.cortes\.....")

FieldListnames = [f.name for f in arcpy.ListFields("C:\Users\daniel.cortes\..")]
ListnamesLeng = len(FieldListnames)
print (ListnamesLeng)
fielditerator = range(2,ListnamesLeng-1)
print (fielditerator)



arcpy.AddField_management("C:\Users\daniel.cortes\..","LLAVE_NESTLE_TIENDAS_VENDEDOR_F", "TEXT",field_length=300)

with arcpy.da.UpdateCursor("C:\Users\daniel.cortes\...",FieldListnames) as cursor:
    for row in cursor:
        ConcList = []
        for i in fielditerator:
            if row[i] != '':
                ConcList.append(row[i])
        row[ListnamesLeng-1]=str(ConcList)
        **print (row[ListnamesLeng-1])**
    cursor.updateRow(row)
0

1 Answer 1

1

cursor.updateRow(row) not being inside the for loop must be causing

StopIteration: iteration not started.

The iteration is already finished when reaching updateRow in the code.

Try indentating it:

with arcpy.da.UpdateCursor("C:\Users\daniel.cortes\...",FieldListnames) as cursor:
    for row in cursor:
        ConcList = []
        for i in fielditerator:
            if row[i] != '':
                ConcList.append(row[i])
        row[ListnamesLeng-1]=str(ConcList)
        cursor.updateRow(row) #Like this

And it looks like you are trying to convert a list to a string by doing str(ConcList). Try join:

row = [1, '', None, 'Hello','']
newval = ', '.join([str(x) for x in row if x]) #"if x" will be False for empty string and None
row[-1] = newval

print(row)
[1, '', None, 'Hello', '1, Hello']
1
  • 1
    Thanks Bud! it works perfect! May 30, 2020 at 2:11

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.