cstaver

Python script won't update field

Discussion created by cstaver on Aug 19, 2011
I'm trying to automate an export procedure that I do regularly.  I tried doing this in Model Builder but whenever I would change or validate the model it would reset the field names I had specified to the default names.  This export will potentially have over 50 fields so that becomes a real problem.  Anyway, I've been trying to script it in Python but I'm not getting very far.  I've put together the two scripts below, which I would ideally like to have in a single script.  The first one exports the data from a personal gdb and adds a label field.  The second script is supposed to update that field.  The first is working well and I think is done.  But the second is a problem.  It always runs, but only sometimes actually updates the field values.  When I get rid of the error handling the problem seems to be coming from line 14.  What baffles me is it works some of the time.  I'd really appreciate if someone that knows this stuff could take a look and let me know what you think.  Thanks.

First Script

try:
    import sys, string, os, arcgisscripting
    gp = arcgisscripting.create(9.3)
    gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Conversion Tools.tbx")
    gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Data Management Tools.tbx")
    
    TaxParcels = "C:/users/corey/desktop/test gdb/testgdb.mdb/TaxParcels"
    Desktop = "C:/Users/Corey/Desktop"
    NewFolder = Desktop + "/Tax Parcels"
    Export = NewFolder + "/TaxParcels.shp"

    #Make feature layer
    gp.MakeFeatureLayer(TaxParcels, "TaxParcelsLayer")  

    # Join layer view to table
    JoinTable = "c:/users/corey/desktop/gisfile/gisfile.dbf"
    gp.AddJoin_management("TaxParcelsLayer", "APN", JoinTable, "YPNO", "KEEP_ALL")
 
    # Create new folder
    gp.CreateFolder_management(Desktop, "Tax Parcels")

    # Export layer to shapefile
    FieldMap = "APN 'APN' true true false 4 Long 0 0 ,None,#,C:/Pershing Co GIS/GIS data/County-maintained/County GIS Gdb/County GIS Gdb.mdb/LandMgmt/TaxParcels,TaxParcels.APN,-1,-1;\
    Status 'Status' true true false 20 Text 0 0 ,None,#,C:/Pershing Co GIS/GIS data/County-maintained/County GIS Gdb/County GIS Gdb.mdb/LandMgmt/TaxParcels,TaxParcels.Status,-1,-1;\
    LastUpdate 'LastUpdate' true true false 8 Date 0 0 ,None,#,C:/Pershing Co GIS/GIS data/County-maintained/County GIS Gdb/County GIS Gdb.mdb/LandMgmt/TaxParcels,TaxParcels.LastUpdate,-1,-1;\
    Comments 'Comments' true true false 255 Text 0 0 ,None,#,C:/Pershing Co GIS/GIS data/County-maintained/County GIS Gdb/County GIS Gdb.mdb/LandMgmt/TaxParcels,TaxParcels.Comments,-1,-1;\
    Acreage 'Acreage' true true false 10 Double 0 0 ,None,#,c:/users/corey/desktop/gisfile/gisfile.dbf,GISFILE.PACRES,-1,-1;"

    gp.FeatureClassToFeatureClass_conversion("TaxParcelsLayer", NewFolder, "TaxParcels.shp", "", FieldMap, "")

    # Add Label field
    gp.addfield (Export, "Label", "text","","",50)   
   
    print "*** Script ran successfully! ***"    

except:
    print "*** Error running script. ***"
    print gp.GetMessages(2)


Second Script

try:
    import sys, string, os, arcgisscripting
    gp = arcgisscripting.create(9.3)

    InputShp = "c:/users/corey/desktop/tax parcels/taxparcels.shp"
      
    # Update Label field
    rows = gp.updatecursor(InputShp)
    row = rows.next()
    while row:
        try:
            row.Label = CreateParcelLabel(row.APN,row.Acreage)
            rows.UpdateRow(row)
            row = rows.Next()
        except:
            row = rows.Next()
    print "*** Script ran successfully! ***"    

except:
    print "*** Error running script. ***"
    print gp.GetMessages(2)

def CreateParcelLabel(APN,Acreage):
    if APN is None:
        APNLabel = ""
    else:
        strAPN = str(APN)
        if len(strAPN) < 6 or len(strAPN) > 7:
            APNLabel = ""
        else:
            APNLabel = strAPN[:-5] + "-" + strAPN[-5:-2] + "-" + strAPN[-2:]
    if Acreage is None:
        AcreageLabel = ""
    else:
        AcreageLabel = str(Acreage) + " ac"
    return APNLabel + "\n" + AcreageLabel

Outcomes