AnsweredAssumed Answered

Python For Loop Error

Question asked by cpotts_extenetsystems on Nov 17, 2016
Latest reply on Nov 21, 2016 by Dan_Patterson

*EDIT*

I realized after posting this question that the code didn't quite come through as formatted as I remember the last time I posted something.  Has the formatting changed, or did I do something wrong?  Thanks!

 

Hello,

 

I have a script that I am using to iterate through a list of all fields in a feature class, run summary statistics on itself to get a table of all entries, and then creates a file geodatabase copy as well as an excel copy of the table.  The tool that I have is working seemingly well, but I end up getting an error at the end. 

 

The code:

# ###################################################################################################
# Name: Database Check
# Author: Coy Potts
# Description: This tool runs a summary on all feature class fields to check for inappropriate entries.
# ###################################################################################################


# ###################################################################################################
# Import system modules
# ###################################################################################################
import arcpy
from arcpy import env
import datetime


# ###################################################################################################
# Set universal variables
# ###################################################################################################
generalFeatures = r'OSP Mapping Layers\General Features'
dateToday = datetime.date.today()
outPath = r'"local_shared_drive"
generalVar = "general"


# ###################################################################################################
# Create the workspace for the statistics documents
# ###################################################################################################

# ########################
# Create a dated folder
# ########################

# Set local variables
datedFolder = str(dateToday)

# Create a dated folder to store the tables
arcpy.CreateFolder_management(outPath, datedFolder)


# ########################
# Create a dated geodatabase
# ########################

# Set local variables
outGDB = str(dateToday) + ".gdb"

# Create a dated file geodatabase
arcpy.CreateFileGDB_management(datedFolder, outGDB, 'CURRENT')


# ###################################################################################################
# Run Statistics on the General Features feature class
# ###################################################################################################

# Set local variables
inFC = generalFeatures
fields = [f.name for f in arcpy.ListFields(inFC)]
fieldCount = len(fields)

i = 0


# ########################
# Iterate through each General Features field, exporting an associated table and excel sheet
# ########################

for f in fields:
    while i < fieldCount:
        # Set local variables
        i += 1
        fieldVar = fields[i]
        statsFields = [[fieldVar,"COUNT"]]
        caseFields = [fieldVar]
        outTable = generalVar + "_" + fieldVar

        # Execute SummaryStatistics
        arcpy.Statistics_analysis(inFC, outTable, statsFields, caseFields)

        # Set local variables
        inTable = inFC
        saveGDB = outPath + str(dateToday) + "/" + outGDB

        # Execute TableToTable
        arcpy.TableToTable_conversion(inTable, saveGDB, outTable, "", "", "")

        arcpy.AddMessage("Summary table created for the " + fieldVar + " field!")

        # Set local variables
        inTable = outTable
        outXLS = outPath + str(dateToday) + "/" + generalVar + "_" + fieldVar + ".xls"

        # Execute TableToExcel
        arcpy.TableToExcel_conversion(inTable, outXLS)

        arcpy.AddMessage("Excel sheet created for the " + fieldVar + " field!")
print("General Features summary tables have been created for all fields!")



 

This script runs through and does exactly what it is meant to do for each field, but then I get this error:

 

I have 4 other feature classes that I would like to eventually add onto this script to run through the exact same process for each of the feature classes, but I can't get it to work on the second feature class with this error getting triggered after the first feature class wraps up.  Any ideas why it is giving me a field type error even though the tool has technically already exported out each of the fields to tables?

 

*SIDE NOTE* (let's fix the problem above first)

The tool needs another minor tweak as well in the fact that in its current state, it creates a copy of the table in my default database before it creates a copy in my created database via the table to table function, which obviously poses the problem of having pre-existing tables when running subsequent times after the first.  I had to try various workarounds when trying to get this to work initially.  For instance, whenever I have an env.workspace declared under the initial while loop's local variables, I would later get an error stating that the table already existed when I tried the table to table function.  However, if I removed the table to table function and relied on the env.workspace table created down in the table to excel function, I would get an error saying that the table couldn't be read. 

Outcomes