Python For Loop Error

2538
25
Jump to solution
11-17-2016 03:04 PM
by Anonymous User
Not applicable

*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. 

0 Kudos
25 Replies
by Anonymous User
Not applicable

The "statsFields" variable is the input for the summary statistics tool. The "fieldVar" variable was meant to solely represent the field that is to be placed in the stats field for the summary statistics tool.  So for each iteration of the loop, whichever field was up in the fieldVar variable would be counted in the stats field. 

That's my thinking anyway.  Is that incorrect?

0 Kudos
DanPatterson_Retired
MVP Emeritus

arcpy.Statistics_analysis(inFC, outTable, statsFields, caseFields)

statsFields is a list of fields.

statsFields = [[fieldVar, "COUNT"]]

is a list of a list of fields

0 Kudos
DarrenWiens2
MVP Honored Contributor

Errrrr Dan, the statsFields parameter is supposed to be a list of lists. It's a list of field-stat pairs, so you can count one field, sum another field, etc.

Summary Statistics—Help | ArcGIS for Desktop 

DanPatterson_Retired
MVP Emeritus

 good catch... I should use the built-in tools more often

0 Kudos
by Anonymous User
Not applicable

Joshua,

Thanks for the resource link.  That does look like a good link to bookmark. 

I have only taken 2 coding courses, one in Java and another in C++. They were also over 10 years ago, though, and I couldn't tell you how to write even the most basic line from either off the top of my head, so I don't think they're playing a part in my missteps.  I'm just simply trying to learn Python as I go.  I'm the first to admit that I'm flying by the seat of my pants with most of my coding, hence my landing here on the forums quite regularly each time I get back into scripting tools. 

About 90% of my code is reverse engineered from the resource documents online, but there are always little nuances to each function, or tidbits that the examples just don't show, that tend to pop up as I go through trying them out in my own scripts.  The forums have been a huge help in pushing me in the right direction. 

0 Kudos
DanPatterson_Retired
MVP Emeritus

I would take some time to see if you need the field itself or the field's name or some other property.

In the following example, the actual field object is collected and is later used to print its name and type.  Sometimes, you need the actual field, sometimes not.

import arcpy

def demo():
    inFC = r"c:\!AV\!Data\Polylines\emst1.shp"
    flds = [i for i in arcpy.ListFields(inFC) if not i.required]
    for i in flds:
        print("Field: {}, type {}".format(i.name, i.type))
    return flds

if __name__ == '__main__':
    flds = demo()

Result

>>> 
Field: ID, type Integer
Field: BETWEEN, type String