For loop with Python for ArcGIS using Add Field and Field Calculator -
For loop with Python for ArcGIS using Add Field and Field Calculator -
i'll seek give brief background here. received big amount of info digitized paper maps. each map saved individual file contains number of records (polygons mostly). goal merge of these files 1 shapefile or geodatabase, easy plenty task. however, other spatial information, records in file not have distinguishing info add together field , populate original file name track provenance. example, in file "505_dmg.shp" each record have "505_dmg" id in column in attribute table labeled "map_name". trying automate using python , sense close. here code i'm using:
# import scheme module import arcpy arcpy import env arcpy.sa import * # set overwrite on/off arcpy.env.overwriteoutput = "true" # define workspace mywspace = "k:/research/data/ads_data/historic/r2_ads_historical_maps/digitized data/arapahoe/test" print mywspace # set workspace listfeatureclass function arcpy.env.workspace = mywspace try: shp in arcpy.listfeatureclasses("","polygon",""): print shp map_name = shp[0:-4] print map_name arcpy.addfield_management(shp, "map_name", "text","","","20") arcpy.calculatefield_management(shp, "map_name","map_name", "python") except: print "fubar, it's not working" print arcpy.getmessages() else: print "you're genius aaron"
the output receive running script:
>>> k:/research/data/ads_data/historic/r2_ads_historical_maps/digitized data/arapahoe/test 505_dmg.shp 505_dmg 506_dmg.shp 506_dmg you're genius aaron
appears successful, right? well, has been...almost: field added , populated both files, , perfect 505_dmg.shp file. problem is, 506_dmg.shp has been labeled "505_dmg" in "map_name" column. though loop appears working partially, map_name variable not seem updating. thoughts or suggestions much appreciated.
thanks,
aaron
i received solution esri give-and-take board: https://geonet.esri.com/thread/114520
basically, little edit in calculate field function did trick. here new code worked:
arcpy.calculatefield_management(shp, "map_name","\"" + map_name + "\"", "python")
python for-loop arcgis
Comments
Post a Comment