Saturday 7 January 2017

cursor - Combining wildcard command along with %s in where clause statement for ArcPy?


I am trying to write a script where the user can search for a five digit zipcode using an input statement and a search cursor. The zipcodes in the file though are nine characters long (i.e. xxxxx-xxxx) so I'm trying to use the wildcard character after five digits in the where clause but I can't figure out either where to put it or the syntax I'm supposed to use. Currently I can get the use input zipcode to print out but when I try putting the % wildcard in I get an error. Can anyone help me?


Here is my code so far:


import arcpy



#set workspace
work=raw_input("work= ")
arcpy.env.workspace=work
zip5=input("zip5= ")
strZip= str(zip5)

def searchHospitals(work,zip5):
fc= "Hospital.shp"
whereClause= '"ZIPCODE" LIKE'+"'%s'"% strZip

print whereClause

Answer



I would use format instead, since % is probably your wildcard character.


def searchHospitals(work,strZip):
fc= "Hospital.shp"
whereClause= '"ZIPCODE" LIKE'+"'{0}%'".format(strZip)

But you can also use double percent signs (%%) in your method:


def searchHospitals(work,strZip):
fc= "Hospital.shp"

whereClause= '"ZIPCODE" LIKE'+"'%s%%'"% strZip

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...