Select to view content in your preferred language

Python Attribute Error 'NoneType' object has no attribute

2450
0
10-19-2010 12:03 PM
SusanZwillinger
Frequent Contributor
I'm working with a script that converts a CSV file to a DBF and then uses the Business Analyst extension to create an XML file that is used for a Gap Report.  The script will SOMETIMES work successfully, but after a successful run I have never been able to get it to run a second time immediately after the first successful run.

During early testing, I was able to close ArcMap after a successful run, restart and then get another successful run, but after 3 successful runs, I now get the error every time that I run the script, even after a reboot.

Initially, I thought that it was a problem with the InsertCursor code that converted the CSV to DBF, but the full error seems to point to something else.  When I get the error, the DBF file is created successfully, but it seems like the software is unable to convert the DBF to an XML file.  One reason could be that the DBF becomes locked.  How do I make sure that the DBF is not locked by the previous code when I try to use it to create the XML?

The line of code that is failing is just one line, but the DBF is obviously being created from the code that completes before this line:
arcpy.CreateProfileByImportFromTable_ba(sSimmonsDBF, "TAP", "COUNT", sPathProTarget)

Here's the error message:
[INDENT][/INDENT]Creating output folders...
[INDENT][/INDENT]Finished creating output folders...
[INDENT][/INDENT]Importing CSV to DBF...
[INDENT][/INDENT]Finished importing CSV to DBF...
[INDENT][/INDENT]Creating target profile ...
[INDENT][/INDENT]Traceback (most recent call last):
  [INDENT][/INDENT]File "C:\My Output Data\script.py", line 154, in <module>
    [INDENT][/INDENT]arcpy.CreateProfileByImportFromTable_ba(sSimmonsDBF, "TAP", "COUNT", sPathProTarget)
  [INDENT][/INDENT]File "C:\Program Files\ArcGIS\Desktop10.0\ArcToolbox\Toolboxes\Business Analyst Tools.tbx", line 3491, in CreateProfileByImportFromTable
[INDENT][/INDENT]AttributeError: 'NoneType' object has no attribute 'CreateProfileByImportFromTable_ba'

[INDENT][/INDENT]Script execution interrupted.[/INDENT]

Here's the part of the script that converts the CSV to DBF and then uses the DBF to create the profile.xml:

[INDENT][/INDENT]arcpy.AddMessage("Importing CSV to DBF...")
    print arcpy.GetMessages()
    # CSV to DBF  ##########################################
    arcpy.CreateTable_management(sPathWork, "SIMMONS.dbf")
   
    #print sSimmonsDBF
    arcpy.AddField_management(sSimmonsDBF, "TAP", "TEXT", "#", "#", "2")
    arcpy.AddField_management(sSimmonsDBF, "BASE", "DOUBLE", "12", "3")
    arcpy.AddField_management(sSimmonsDBF, "XTRA", "DOUBLE", "12", "3")
    arcpy.AddField_management(sSimmonsDBF, "COUNT", "DOUBLE", "12", "3")
    arcpy.DeleteField_management(sSimmonsDBF, "Field1")
   
    inf=open(sTapCSV, "r")
   
    rows = arcpy.InsertCursor(sSimmonsDBF)

    for i in range(76):
        print i
        line = inf.readline()
        print line
        if i > 9:
            l = len(line)
            f = line.find('(')
            value = line[f+1:l]
            value = value.replace(')','')
            value = value.replace('"','')
            tokens = string.split(value,',')
            row = rows.newRow()
            row.TAP=tokens[0]
            row.BASE=float(tokens[1])*1000
            row.XTRA=float(tokens[2])
            row.COUNT=float(tokens[3])*1000
            rows.insertRow(row)

    inf.close()
    del rows
    del inf
   
    arcpy.AddMessage("Finished importing CSV to DBF...")
    print arcpy.GetMessages()

    arcpy.AddMessage("Creating target profile ...")
    print arcpy.GetMessages()
   

    # DBF to Target Profile.xml  ###################################################
    arcpy.CreateProfileByImportFromTable_ba(sSimmonsDBF, "TAP", "COUNT", sPathProTarget)
    arcpy.AddMessage("Finished creating target profile ...")
    print arcpy.GetMessages()

We're using Python 2.6 with ArcGIS 10.  It seems like this could be related to a Python bug (http://bugs.python.org/issue5762) that other people have experience in different circumstances (not with ArcGIS), but being new to this, I'm not sure.
0 Kudos
0 Replies