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))
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Jun 26, 2012 at 13:25

1 Answer 1

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))
answered Jun 26, 2012 at 14:16
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.