4

I have created a python script that batch summarizes all string fields within a fc table based on the concatenation of two fields. The script loops through a list of the fields, however it stops about halfway through (same spot each time). Why does this code not process the remaining string fields within the list? After running it once, if I delete all the fields that it processed correctly the first time, then run it again on only the fields that it did not process, it runs ok. Maybe my issue is with this line:

fieldname = flist.pop(0)

Ideas?

See code below

import arcpy, os
from arcpy import env

try:

    # Local variables:
    inTable = arcpy.GetParameterAsText(0)
    outWKSpace = arcpy.GetParameterAsText(1)

    # Process: Add Field
    arcpy.AddField_management(inTable, "cat", "TEXT", "", "", "200", "", "NULLABLE", "NON_REQUIRED", "")

    flist = arcpy.ListFields(inTable, '', 'String')
    fieldname = ''

    for field in flist:
        fieldname = flist.pop(0)
        arcpy.CalculateField_management(inTable, "cat", "!Sub_Batch!+\" \"+ !"+ fieldname.name +"!", "PYTHON_9.3", "")
        outTable = outWKSpace + "\SUM_" + fieldname.name
        table2 = arcpy.Frequency_analysis(inTable, outTable, "cat", "")

        intCount = arcpy.GetCount_management(outWKSpace + "\SUM_" + fieldname.name)

        arcpy.AddMessage(fieldname.name + ' records = ' + str(intCount))

except Exception, e:
  import traceback
  map(arcpy.AddError, traceback.format_exc().split("\n"))
  arcpy.AddError(str(e))
2

You're right - the problem was with fieldname.pop(0) - this actually takes the object at index 0 and removes it from the list. But you're already iterating through the list with for field in flist. Your code should be instead:

for field in flist:
    arcpy.CalculateField_management(inTable, "cat", "!Sub_Batch!+\" \"+ !"+ field.name +"!", "PYTHON_9.3", "")
    outTable = outWKSpace + "\SUM_" + field.name
    table2 = arcpy.Frequency_analysis(inTable, outTable, "cat", "")

    intCount = arcpy.GetCount_management(outWKSpace + "\SUM_" + field.name)

    arcpy.AddMessage(field.name + ' records = ' + str(intCount))

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.