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
The first error message is telling you what's wrong. You trying to use a variable materialType, but haven't defined it. You first need a statement like materialType = 'plastic'. I'm also not sure you should have the % sign right next to the variable name - % materialType instead of %materialType. Maybe that's ok.
You may want to consider the more recent method of formatting strings:
Qry = "Material = {0}".format(materialType)
arcpy.SearchCursor(fc,Qry, "", "Material; Diameter; System")
fields = "Material; Diameter; System" arcpy.SearchCursor(fc,Qry, "", fields)
Try printing Qry right after you set it to make sure it has the value you want. Maybe print other values as well.
For Get ParameterByText to work, you need to have made the script a tool, or run it from the command line with arguments. I recommend hard coding some values in for testing. Once it works there, you can switch to parameters.
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 Class fields
Field = ["Material", "Diameter", "System]"
# Establish materials search for tool
Qry ="Material = Plastic"
# Establish Class fields
Field = ["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")
# Create cursor to search gas mains by material
arcpy.da.SearchCursor(fc,Qry, fields) as Qry:
for row in Qry:
rptMat = str(row.getValue("Material"))
rptDiam = str(row.getValue("Diameter"))
rptSys = str(row.getValue("System"))
file.write(rptMat + " " + rptDiam + " " + rptSys + "\n")
del Qry, row
Qry ="'Material' = \"Plastic\""
tryQry ="'Material' = \"Plastic\""
with arcpy.da.SearchCursor(fc, fields, Qry) as cursor: """(you don't want to use Qry, since that variable is already declared) """
for row in cursor:
rptMat = str(row.getValue("Material"))
rptDiam = str(row.getValue("Diameter"))
rptSys = str(row.getValue("System"))