How do I go about using a variable from a raw_input for the where clause in da.SearchCursor?
I can't seem to get the syntax correct
Code:
import arcpy
from arcpy import env
env.workspace=r"F:\GEOG_488\Final_Project_Data\FinalProject488.gdb"
fc="PARCELS" #Create variable for feature class
tc="ADDRESS_TEST_20" #Create variable for table
HouseNumber = int(raw_input("Enter Address Number: ")) #User input House Number
print "Situs Addresses with "+ str(HouseNumber)+" for the Situs Number:"
cursor = arcpy.da.SearchCursor(tc,["ADDR"],'"SA_House_Number"= HouseNumber') #Create search cursor where records = HouseNumber
for row in cursor:
print "Situs Address with House Number"+str(HouseNumber)":",row[0]
del row, cursor
Thanks for any help.
Solved! Go to Solution.
'"SA_House_Number" = ' + str(HouseNumber)
When the HouseNumber is inside the quotes it is not recognized as variable
I am not sure you have to put the attribute name (SA_House_Number) in quotes in the new versions.
Escape HouseNumber from the where clause string.
'"SA_House_Number" = ' + str(HouseNumber)
When the HouseNumber is inside the quotes it is not recognized as variable
I am not sure you have to put the attribute name (SA_House_Number) in quotes in the new versions.
I encourage you to learn about Python string formatting (Format String Syntax). It is extremely robust.
cursor = arcpy.da.SearchCursor(tc,["ADDR"],"SA_House_Number = {}".format(HouseNumber))
Thank you all for your help. It now works using both the suggestion from Mody and Joshua.
I'm now reading up on string formatting.
Thanks again!
Ryan
Joshua Bixby deserves a beer or milk shake if a teetotaler.
+1 for string formatting.