I was given a list of addresses without any zip codes, and I'm trying to write a python snippet that will loop through a set of possible zip codes and make a set of geocoded shapefiles that I will later merge.
My address table is essentially laid out like this: ("Project_" fields are the possible zip codes)
Address City Project_1 Project_2 ... Project_42
123 Main Street Birmingham 78954 78953 ... 78961
My python snippet looks like this:
import arcpy
from arcpy import env
env.workspace = "N:\Reference\GIS\State\Project\Address Locator"
geocoder = env.workspace + "\State_Addresses_Create_Addr"
for y in range(1,43):
zippo = "Projects_" + str(y)
outfile = '"N:\Project_Geocode_Results_'+ str(y) + '"'
fncgeo = '"Street Apartment VISIBLE NONE; ZIP ' + zippo + ' VISIBLE"'
arcpy.GeocodeAddresses_geocoding("Geocode_Table", geocoder, fncgeo, outfile)
When I try to run the snippet outside of a loop (i.e., based on a single field), the geocode works fine. When I run it as written above, I get:
ERROR 000010: Geocode addresses failed. Failed to execute (Geocode Addresses).
Does anyone have any ideas as to why the geocode won't work in a loop?
-
One other thing that might be contributing to the problem is the backslash character... for simplicity I always use the forward slash in paths, even though in Windows it doesn't look right... anywhere there is a back slash, change it to a forward slash...Jason Miller– Jason Miller2014年09月09日 20:24:43 +00:00Commented Sep 9, 2014 at 20:24
1 Answer 1
I think the extra double quote is the issue in the outfile and fncgeo variable. Try this:
import arcpy
from arcpy import env
env.workspace = "N:\Reference\GIS\State\Project\Address Locator"
geocoder = env.workspace + "\State_Addresses_Create_Addr"
for y in range(1,43):
zippo = "Projects_" + str(y)
outfile = r'N:\Project_Geocode_Results_{0}.shp'.format(y)
fncgeo = 'Street Apartment VISIBLE NONE; ZIP {0} VISIBLE'.format(zippo)
arcpy.GeocodeAddresses_geocoding("Geocode_Table", geocoder, fncgeo, outfile)
-
Thanks! Your suggestion made the loop work; only problem now is that the background server is throwing an exception on the third or fourth loop every time. It won't run in the foreground either--crashes after three or four loops. But your fix definitely got the loop going--thanks!Jon Boyette– Jon Boyette2014年09月10日 22:29:02 +00:00Commented Sep 10, 2014 at 22:29
-
I am not sure why you are getting that error, especially after it completes a few iterations of the loop. Are the few outputs that do get created correct? Maybe you could add a time.sleep(5) after the geocode. Not sure if that would help or not but could be worth a try.crmackey– crmackey2014年09月11日 13:27:47 +00:00Commented Sep 11, 2014 at 13:27
-
Thanks again! As you suggested, I added time.sleep(5) to the end of the loop, and it looped all the way to the end of the range. I guess Arc just needed its beauty rest.Jon Boyette– Jon Boyette2014年09月11日 20:39:18 +00:00Commented Sep 11, 2014 at 20:39
-
Wow, that is funny. I cannot believe that worked. I'm glad you're up and running now.crmackey– crmackey2014年09月11日 21:17:03 +00:00Commented Sep 11, 2014 at 21:17