I have a search cursor that using a SQL query to match addresses and then returns the associated roll number with that address. However, some addresses do not match the roll number and then I believe the search cursor stops. Is there a bit of code I can throw in that will continue with the next address even if a match isn't found? I think an 'if' statement would work well, but I don't know what to write for it!
I'm having trouble generating this. My code is iterating through foldernames to get the address and querying a parcel layer.
I can post the code later, if you need an example.
import arcpy
import os
import re
import sys
import traceback
import collections
basedir = r"C:\Test"
fc = r"G:\ParcelsFixNames.shp"
field1 = "ADD1"
field2 = "STREETNAME"
try:
#find all foldernames in directory
for fn in os.listdir(basedir):
string = str(fn)
#split name at hyphen
if re.findall('-', fn):
#right side of hyphen is street address, leftside is street name
lhs, rhs = string.split('-')
street = str.lstrip(lhs)
street = str.replace(street,'.','')
street = str.rstrip(street)
street = str.upper(street)
add = str.lstrip(rhs)
expression3 = '"{field1}" = \'{add}\' AND "{field2}" LIKE \'%{street}%\''.format(field1=field1, add=add, field2=field2, street=street)
rows = arcpy.SearchCursor(fc, fields="STREETNAME; ARN; ADD1", where_clause=expression3)
for row in rows:
Roll = row.getValue("ARN")
newname = str(Roll)
newpath = os.path.join(basedir, newname)
#if the name exists already add _1 to end
if os.path.exists(newpath):
os.rename(os.path.join(basedir, fn),
os.path.join(basedir, newname + "_" + "1"))
else:
os.rename(os.path.join(basedir, fn),
os.path.join(basedir, newname))
else:
print add + " " + street + " " + "Already exists...passing"
continue
except:
print ("Error occurred")
tb = sys.exc_info()[2]
tbinfo = traceback.format_tb(tb)[0]
pymsg = "PYTHON ERRORS:\nTraceback Info:\n" + tbinfo + "\nError Info:\n " + \
str(sys.exc_type)+ ": " + str(sys.exc_value) + "\n"
msgs = "GP ERRORS:\n" + arcpy.GetMessages(2 )+ "\n"
print (pymsg)
print (msgs)
for row in rows
. What version of ArcMap are you using? – Paul Jul 19 '13 at 14:00