eolsonmurphy

Error using SearchCursor to export values to text file

Discussion created by eolsonmurphy on Jun 13, 2014
Latest reply on Jun 17, 2014 by eolsonmurphy
I have created a very basic script (with the help of others) that uses a searchCursor to pull data from a feature class and, in theory, add that data to a text file.
I think I almost have it but I am still getting a few errors that I cannot figure out (I am completely new to python and am at a complete loss now).

The first error I get is as follows:
Traceback (most recent call last):
File "G:\Olson-Murphy_Week10\Scripts\MaterialSearch2.py", line 10, in <module>
Qry = "Material = " + '%s' %materialType
NameError: name 'materialType' is not defined

Failed to execute (MaterialReport).

By removing "%materialType" from the script i get this error to go away but then I am faced with another:

Traceback (most recent call last):
File "G:\Olson-Murphy_Week10\Scripts\MaterialSearch2.py", line 14, in <module>
MaterialSearch = arcpy.SearchCursor(fc,Qry, fields = "Material;Diameter;System")
File "c:\program files\arcgis\desktop10.1\arcpy\arcpy\__init__.py", line 1133, in SearchCursor
return gp.searchCursor(dataset, where_clause, spatial_reference, fields, sort_fields)
File "c:\program files\arcgis\desktop10.1\arcpy\arcpy\geoprocessing\_base.py", line 359, in searchCursor
self._gp.SearchCursor(*gp_fixargs(args, True)))
RuntimeError: ERROR 999999: Error executing function.

Failed to execute (MaterialReport).

My code can be found below. Anyone have any suggestions? (this code someone else did say worked on their machine so I am at a total loss)


import arcpy
# Set workspace
arcpy.env.workspace = r"G:\Olson-Murphy_Week10\SouthFloridaNaturalGas.gdb"


#Estabslish input feature class
fc = r"G:\Olson-Murphy_Week10\SouthFloridaNaturalGas.gdb\Mains"

# Establish materials search for tool
Qry = "Material = " + '%s' %materialType


# Create cursor to search gas mains by material
MaterialSearch = arcpy.SearchCursor(fc,Qry, fields = "Material;Diameter;System")

#Define output
outReport = arcpy.GetParameterAsText(1)

#Open the report text file in write mode
file = open (outReport, "w")

#Add Header lines to report text file
file.write("Mains found in this report include:\n")
file.write("Material:" + Qry + "\n")

#Results of SearchCursor
for row in MaterialSearch:
    rptMat = str(row.getValue("Material"))
    rptDiam = str(row.getValue("Diameter"))
    rptSys = str(row.getValue("System"))

    file.write(rptMat + " " + rptDiam + " " + rptSys + "\n")

del MaterialSearch, row

Outcomes