Python For Loop Error

1320
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
1 Solution

Accepted Solutions
NeilAyres
MVP Frequent Contributor

Is this the same line 77 as in the script above. I don't think so somehow.

Firstly in you script, you don't need the whole while loop. For f in fields will already iterate through the fields in the fields list. So you don't need i. And the whole of the while inner block should come in one indentation.

for field in fields:

    statsFields = [[field,"COUNT"]]
    caseFields = [field]
    outTable = generalVar + "_" + field

    # 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 " + field + " field!")

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

    # Execute TableToExcel
    arcpy.TableToExcel_conversion(inTable, outXLS)

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

As this appears to be run inside the python window in ArcMap, you don't need all the addmessage stuff either. I suppose you want to turn this into a script tool, but this is not doing anything at the moment.

Just use some more print statements. What does the list fields look like at the moment?

What is line 77 of the current script?

View solution in original post

25 Replies
DarrenWiens2
MVP Honored Contributor

I think you need fieldVar.name (a string) rather than fieldVar (a field object).

by Anonymous User
Not applicable

Even though the script runs through the summary of each field and exports an associated database table and excel sheet, as intended?  This process iterates through 23 separate fields before sending back this error, but the very last field in the feature class has exported just fine.  I'll give your suggestion a try, just seems strange given that it works appropriately up until the end.

I'm at home and can't test right now, but I was thinking maybe it had to do with my while loop.  There are 23 fields total, but that's counting all default fields as well, so the very last field is technically a shape field, thus possibly kicking back the error...?  I'll have to try tomorrow when I'm back in the office. 

0 Kudos
NeilAyres
MVP Frequent Contributor

Is the input a feature class, ie has a geometry column?

Perhaps you are trying the generate statistics on a field which makes no sense.

by Anonymous User
Not applicable

Yes, it's a feature class.  That's what I referencing in my comment above.  Maybe it gets through all the other fields, but then once it hits that last field then it doesn't know what to do because it's a shape field. 

0 Kudos
NeilAyres
MVP Frequent Contributor

So, why not try :

fields = [f.name for f in arcpy.ListFields(inFC) if not f.required]

To skip over the OID, SHAPE@ etc

by Anonymous User
Not applicable

I will give that a try tomorrow as well.  Just so I understand, is f.required literal, or would I need to define something there?  I have never seen that one before is why I ask. 

Thanks!

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

It is in the help Field—Help | ArcGIS for Desktop 

everything about arcpy is found here at the top of its help file tree What is ArcPy?—Help | ArcGIS for Desktop 

by Anonymous User
Not applicable

Thanks, Dan!  I'll bookmark that resource. 

0 Kudos
NeilAyres
MVP Frequent Contributor

f.required is a property of f (field object) and is True or False.

Hence

if not f.required

So you don't need anything else.