Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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))

thank you!

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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))
share|improve this answer
    
Great, thank you! –  artwork21 Jun 26 '12 at 14:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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