<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Help needed: Python looping in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/help-needed-python-looping/m-p/593423#M46509</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Dan, thanks for the observations.&amp;nbsp; I implemented them but to no avail, still having the same problem.&amp;nbsp; Script is "working" but not populating the second file's field correctly.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 07 Oct 2014 00:55:30 GMT</pubDate>
    <dc:creator>AaronSidder</dc:creator>
    <dc:date>2014-10-07T00:55:30Z</dc:date>
    <item>
      <title>Help needed: Python looping</title>
      <link>https://community.esri.com/t5/python-questions/help-needed-python-looping/m-p/593421#M46507</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;SPAN style="font-family: calibri,verdana,arial,sans-serif; font-size: 12pt;"&gt;Hello,&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: calibri,verdana,arial,sans-serif; font-size: 12pt;"&gt;I'll try to give a brief background here.&amp;nbsp; I recently received a large amount of data that was all digitized from paper maps.&amp;nbsp; Each map was saved as an individual file that contains a number of records (polygons mostly).&amp;nbsp; My goal is to merge all of these files into one shapefile or geodatabase, which is an easy enough task.&amp;nbsp; However, other than spatial information, the records in the file do not have any distinguishing information so I would like to add a field and populate it with the original file name to track its provenance.&amp;nbsp; For example, in the file "505_dmg.shp" I would like each record to have a "505_dmg" id in a column in the attribute table labeled "map_name".&amp;nbsp; I am trying to automate this using Python and feel like I am very close.&amp;nbsp; Here is the code I'm using:&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;# Import system module&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;import arcpy&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;from arcpy import env&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;from arcpy.sa import *&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;# Set overwrite on/off&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;arcpy.env.overwriteOutput = "TRUE"&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;# Define workspace&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;mywspace = "K:/Research/DATA/ADS_data/Historic/R2_ADS_Historical_Maps/Digitized Data/Arapahoe/test"&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;print mywspace&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;# Set the workspace for the ListFeatureClass function&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;arcpy.env.workspace = mywspace&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;try:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for shp in arcpy.ListFeatureClasses("","POLYGON",""):&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print shp&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; map_name = shp[0:-4]&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print map_name&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddField_management(shp, "map_name", "TEXT","","","20")&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management(shp, "map_name","map_name", "PYTHON")&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;except:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Fubar, It's not working"&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print arcpy.GetMessages()&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;else:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "You're a genius Aaron"&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: calibri,verdana,arial,sans-serif; font-size: 12pt;"&gt;The output I receive from running this script:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;K:/Research/DATA/ADS_data/Historic/R2_ADS_Historical_Maps/Digitized Data/Arapahoe/test&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;505_dmg.shp&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;505_dmg&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;506_dmg.shp&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;506_dmg&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;You're a genius Aaron&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: calibri,verdana,arial,sans-serif; font-size: 12pt;"&gt;Appears successful, right?&amp;nbsp; Well, it has been: a field was added and populated, and it is perfect for 505_dmg.shp.&amp;nbsp; Problem is. 506_dmg.shp is also been labeled "505_dmg" in the "map_name" column.&amp;nbsp; Though the loop appears to be working, the map_name variable does not seem to be updating.&amp;nbsp; Any thoughts or suggestions much appreciated.&amp;nbsp; Files attached in the zip file.&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: calibri,verdana,arial,sans-serif; font-size: 12pt;"&gt;Thanks,&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: calibri,verdana,arial,sans-serif; font-size: 12pt;"&gt;Aaron&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 07 Oct 2014 00:23:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/help-needed-python-looping/m-p/593421#M46507</guid>
      <dc:creator>AaronSidder</dc:creator>
      <dc:date>2014-10-07T00:23:42Z</dc:date>
    </item>
    <item>
      <title>Re: Help needed: Python looping</title>
      <link>https://community.esri.com/t5/python-questions/help-needed-python-looping/m-p/593422#M46508</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;on a lark...rename the field you are creating so that it isn't the same as your variable name...also, fix your indentation use try not to mix and match...it makes it hard to read...use &lt;SPAN class="s"&gt;"PYTHON_9.3" as well ... no time to test, but just some observations and probably not the solution&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 07 Oct 2014 00:47:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/help-needed-python-looping/m-p/593422#M46508</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2014-10-07T00:47:35Z</dc:date>
    </item>
    <item>
      <title>Re: Help needed: Python looping</title>
      <link>https://community.esri.com/t5/python-questions/help-needed-python-looping/m-p/593423#M46509</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Dan, thanks for the observations.&amp;nbsp; I implemented them but to no avail, still having the same problem.&amp;nbsp; Script is "working" but not populating the second file's field correctly.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 07 Oct 2014 00:55:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/help-needed-python-looping/m-p/593423#M46509</guid>
      <dc:creator>AaronSidder</dc:creator>
      <dc:date>2014-10-07T00:55:30Z</dc:date>
    </item>
    <item>
      <title>Re: Help needed: Python looping</title>
      <link>https://community.esri.com/t5/python-questions/help-needed-python-looping/m-p/593424#M46510</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Aaron,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Try using this instead, worked for me.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;arcpy.CalculateField_management(shp, "map_name","\"" + map_name + "\"", "PYTHON")&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;IMG alt="untitled.bmp" class="jive-image image-1" src="https://community.esri.com/legacyfs/online/17413_untitled.bmp" style="width: 620px; height: 496px;" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 07 Oct 2014 00:56:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/help-needed-python-looping/m-p/593424#M46510</guid>
      <dc:creator>RiyasDeen</dc:creator>
      <dc:date>2014-10-07T00:56:59Z</dc:date>
    </item>
    <item>
      <title>Re: Help needed: Python looping</title>
      <link>https://community.esri.com/t5/python-questions/help-needed-python-looping/m-p/593425#M46511</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi &lt;A href="https://community.esri.com/migrated-users/55033"&gt;Riyas Deen&lt;/A&gt;,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you so much, that appears to have done the trick.&amp;nbsp; Can I ask what exactly that does to the code?&amp;nbsp; What do the quotes and slash add to what I originally had?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Again, thank you. &lt;/P&gt;&lt;P&gt;Aaron&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 07 Oct 2014 01:06:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/help-needed-python-looping/m-p/593425#M46511</guid>
      <dc:creator>AaronSidder</dc:creator>
      <dc:date>2014-10-07T01:06:32Z</dc:date>
    </item>
    <item>
      <title>Re: Help needed: Python looping</title>
      <link>https://community.esri.com/t5/python-questions/help-needed-python-looping/m-p/593426#M46512</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Aaron,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This &lt;STRONG&gt;"\""&lt;/STRONG&gt; would actually translate to a double quote in the script. The code is actually prefixing and suffixing the shape file name with double quotes. This will pass the shape file name as a string to Calculate Field tool.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 07 Oct 2014 01:20:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/help-needed-python-looping/m-p/593426#M46512</guid>
      <dc:creator>RiyasDeen</dc:creator>
      <dc:date>2014-10-07T01:20:02Z</dc:date>
    </item>
    <item>
      <title>Re: Help needed: Python looping</title>
      <link>https://community.esri.com/t5/python-questions/help-needed-python-looping/m-p/593427#M46513</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Great, thanks for explanation&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 07 Oct 2014 01:26:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/help-needed-python-looping/m-p/593427#M46513</guid>
      <dc:creator>AaronSidder</dc:creator>
      <dc:date>2014-10-07T01:26:11Z</dc:date>
    </item>
    <item>
      <title>Re: Help needed: Python looping</title>
      <link>https://community.esri.com/t5/python-questions/help-needed-python-looping/m-p/593428#M46514</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;happily it is far easier...your calculatefield expression was correct, you just forgot to make a variable name out of it...see the example below&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;gt;&amp;gt;&amp;gt; import arcpy&lt;/P&gt;&lt;P&gt;&amp;gt;&amp;gt;&amp;gt; shp = r"C:\Temp\SoObviousNow.shp"&lt;/P&gt;&lt;P&gt;&amp;gt;&amp;gt;&amp;gt; arcpy.AddField_management(shp, "HereYaGo", "TEXT","","","40")&lt;/P&gt;&lt;P&gt;&amp;lt;Result 'C:\\Temp\\SoObviousNow.shp'&amp;gt;&lt;/P&gt;&lt;P&gt;&amp;gt;&amp;gt;&amp;gt; map_name = shp[0:-4]&lt;/P&gt;&lt;P&gt;&amp;gt;&amp;gt;&amp;gt; print map_name&lt;/P&gt;&lt;P&gt;C:\Temp\SoObviousNow&lt;/P&gt;&lt;P&gt;&amp;gt;&amp;gt;&amp;gt; arcpy.CalculateField_management(shp, "HereYaGo","map_name", "PYTHON_9.3")&lt;/P&gt;&lt;P&gt;&amp;lt;Result 'C:\\Temp\\SoObviousNow.shp'&amp;gt;&lt;/P&gt;&lt;P&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;IMG alt="SoObviousNowImg.png" class="jive-image image-1" height="448" src="https://community.esri.com/legacyfs/online/18091_SoObviousNowImg.png" style="width: 212px; height: 207px; float: left;" width="436" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 07 Oct 2014 05:08:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/help-needed-python-looping/m-p/593428#M46514</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2014-10-07T05:08:20Z</dc:date>
    </item>
  </channel>
</rss>

