mrskyp

Problem hosting Python Script in ArcCatalog

Discussion created by mrskyp on Jun 18, 2012
I'm using ArcMap/ArcEditor 10.0 with Python 2.6.5. I have a problematical Python script which reads in a CSV file and populates an existing Personal Geodatabase table:

(start of my code)
import arcpy, csv

TypeOfData = arcpy.GetParameterAsText(0)
inputCSVfile = arcpy.GetParameterAsText(1)
FeatureClassTable = arcpy.GetParameterAsText(2)

rows = arcpy.InsertCursor(FeatureClassTable)
fieldname = ["Last_Update","Last_Update_By","Feature_ID","Survey_ID","Survey_ID_Ref","Survey_Name","Feature_Name","Feature_Desc",
             "Symbology_Code","Symbology_Name","Line_ID","Line_Name","Easting","Northing","Depth","Altitude","Time_Stamp",
             "Heading","Remarks"]

#inputTxtFile = open("C:\GeneralWorkData\ArcGIS Test Data\OGP_BP\Python_work\BP8719_Test_PointAttribs.csv","rb")
inputTxtFile = open(inputCSVfile,"rb")
reader = csv.reader(inputTxtFile)
ID = 0

try:
  for inrow in inputTxtFile.readlines():
    if ID>0:
      itemstr = inrow.split(",")
      x = itemstr[10]
      y = itemstr[11]
      z = itemstr[12]
      m = 0
      point = arcpy.Point(x,y,z,m,ID)
      pnt = arcpy.PointGeometry(point)
      row = rows.newRow()
      row.setValue("Shape",pnt)
      row.setValue(fieldname[0],itemstr[0])
      row.setValue(fieldname[1],itemstr[1])
      if itemstr[2]!='': row.setValue(fieldname[3],itemstr[2])
      if itemstr[2]=='': row.setValue(fieldname[3],0)
      row.setValue(fieldname[4],itemstr[3])
      row.setValue(fieldname[5],itemstr[4])
      row.setValue(fieldname[6],itemstr[5])
      row.setValue(fieldname[7],itemstr[6])
      row.setValue(fieldname[8],itemstr[7])
      row.setValue(fieldname[10],itemstr[8])
      row.setValue(fieldname[11],itemstr[9])
      row.setValue(fieldname[12],itemstr[10])
      row.setValue(fieldname[13],itemstr[11])
      row.setValue(fieldname[14],itemstr[12])
      row.setValue(fieldname[15],itemstr[13])
      row.setValue(fieldname[16],itemstr[14])
      row.setValue(fieldname[17],itemstr[15])
      row.setValue(fieldname[18],itemstr[16])
      rows.insertRow(row)
      if ID%500==0: arcpy.AddMessage(ID)
    ID += 1
  arcpy.AddMessage(ID-1)  
  inputTxtFile.close()
  del rows
  del row

except Exception, Argument:
  arcpy.AddMessage(Argument)
  if inputTxtFile: inputTxtFile.close()
  del rows
  del row

(end of my code)

Opt.1: If I run this script in catalog (right click scriptname>debug), it runs perfectly - populates the (initially empty) PGDB table, closes open files. I can immediately view the data in ArcMap. No problems, no errors, but it takes ages. In PythonWin editor, variable FeatureClassTable seems valued correctly with double backslashes, outer quotes preceded by lowercase u as displayed in PythonWin Watchwindow - presumably meaning unicode??

Opt.2: If I run in edit mode (catalog>rightclick scriptname>edit>run), it produces the following error:

Traceback (most recent call last):
  File "C:\Program Files\ArcGIS\Desktop10.0\ArcToolbox\Scripts\SampleTools\Sample Tools\Scripts\OGP_BP.py", line 9, in <module>
    rows = arcpy.InsertCursor(FeatureClassTable)
  File "C:\Program Files\ArcGIS\Desktop10.0\ArcPy\arcpy\__init__.py", line 837, in InsertCursor
    return gp.insertCursor(*args)
  File "C:\Program Files\ArcGIS\Desktop10.0\ArcPy\arcpy\geoprocessing\_base.py", line 367, in insertCursor
    self._gp.InsertCursor(*gp_fixargs(args)))
RuntimeError: ERROR 999999: Error executing function.


Opt.3: If I run it straight from the scriptname in catalog (catalog>scriptname>doubleclick), it runs fine and quickly, but doesn't close the PGDB properly (the .ldb lockfile remains). How come it doesn't?


I want to run the script per option 3 - the other two methods perhaps will help me learn something about Python / Arc10 environment. How come I get different results from the three ways of running the same script?

Many thanks,
Rob Pearce

Outcomes