<?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: Changing field names in python script in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/changing-field-names-in-python-script/m-p/633093#M49333</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;I want to:&lt;BR /&gt;* Read the list of fields in a shapefile&lt;BR /&gt;* check for a field starting with "Pri*"&lt;BR /&gt;* If it is not named "Priority", I want to rename them to "Priority"&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;A far more efficient way to complete this operation is to use the Merge_management tool with a field map to make a new copy of your shapefile with the field names and order just the way you want them.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The FieldMappings and FieldMap objects are a little tricky but not too difficult once you figure them out.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 13 May 2014 22:03:11 GMT</pubDate>
    <dc:creator>curtvprice</dc:creator>
    <dc:date>2014-05-13T22:03:11Z</dc:date>
    <item>
      <title>Changing field names in python script</title>
      <link>https://community.esri.com/t5/python-questions/changing-field-names-in-python-script/m-p/633088#M49328</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello All,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The short and sweet version is this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I want to: &lt;/SPAN&gt;&lt;BR /&gt;&lt;OL&gt;&lt;BR /&gt;&lt;LI&gt;Read the list of fields in a shapefile&lt;/LI&gt;&lt;BR /&gt;&lt;LI&gt;check for a field starting with "Pri*"&lt;/LI&gt;&lt;BR /&gt;&lt;LI&gt;If it is not named "Priority", I want to rename them to "Priority"&lt;/LI&gt;&lt;BR /&gt;&lt;/OL&gt;&lt;SPAN&gt;I use the Priority field as the Case field in the next tool (summary stats) and&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;some of the shapefiles have the field as "Priority_1" or something similar.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;prifield = arcpy.ListFields(preparedfeatures, 'Pri*')
 try:
&amp;nbsp; if prifield != "Priority":
&amp;nbsp;&amp;nbsp; arcpy.AddWarning('The priority field was named ' + str(prifield) + " and will be renamed.")
&amp;nbsp;&amp;nbsp; arcpy.AddField_management(preparedfeatures, "Priority", "SHORT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management(preparedfeatures, "Priority", prifield , "PYTHON_9.3", "")
 except:
&amp;nbsp; arcpy.AddWarning(arcpy.GetMessages())
&amp;nbsp; continue&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;What prints in the console is this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;"The priority field was named [&amp;lt;Field object at 0x19b708f0[0x18f993b0]&amp;gt;] and will be renamed."&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So clearly it is not reading the field names properly.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I have read about arcpy.Alterfield_management, but it reportedly does not work with shapefiles.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I have also tried iterating over the fields using the field index and looking for the field name I want, but no luck.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks for your help.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 13 May 2014 16:22:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/changing-field-names-in-python-script/m-p/633088#M49328</guid>
      <dc:creator>CharliePeterson</dc:creator>
      <dc:date>2014-05-13T16:22:53Z</dc:date>
    </item>
    <item>
      <title>Re: Changing field names in python script</title>
      <link>https://community.esri.com/t5/python-questions/changing-field-names-in-python-script/m-p/633089#M49329</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi Charlie&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;A few things.&amp;nbsp; First off, prifield = arcpy.ListFields(preparedfeatures, 'Pri*'), makes prifield a list filled with all the fields that meet your parameters.&amp;nbsp; To get individual fields from this list you need to use a for loop or indexes.&amp;nbsp; Secondly, it is not incorrect what it was returning, it was returning the space on your hard drive where the data is stored.&amp;nbsp; To get the actual name value of the field you need to call .baseName on it or .aliasName&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So you would need something like &lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
prifield = arcpy.ListFields(preparedfeatures, 'Pri*')
for field in prifield:
&amp;nbsp; if field.baseName != "Priority":
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddWarning('The priority field was named ' + str(field.baseName) + " and will be renamed.")
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddField_management(preparedfeatures, "Priority", "SHORT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management(preparedfeatures, "Priority", field , "PYTHON_9.3", "")

&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;more on getting info about fields can be found here&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.2/index.html#//018z0000004n000000" rel="nofollow noopener noreferrer" target="_blank"&gt;http://resources.arcgis.com/en/help/main/10.2/index.html#//018z0000004n000000&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Hope this helps you.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 02:56:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/changing-field-names-in-python-script/m-p/633089#M49329</guid>
      <dc:creator>IanMurray</dc:creator>
      <dc:date>2021-12-12T02:56:11Z</dc:date>
    </item>
    <item>
      <title>Re: Changing field names in python script</title>
      <link>https://community.esri.com/t5/python-questions/changing-field-names-in-python-script/m-p/633090#M49330</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Great info! &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Only issue now is transferring the values from the old field to the new one&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;How do I get the values from the original field to transfer over to the new field?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Again, I would ideally like to just rename the field, any ideas on that?&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I tried adding&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;field.baseName = "Priority" after the If. Still no luck&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
 try:
&amp;nbsp; arcpy.Statistics_analysis(preparedfeatures, sumstattblnum, "AREA_GEO SUM", "Priority")
 except:
&amp;nbsp;&amp;nbsp; for field in fields:
&amp;nbsp;&amp;nbsp; if "Pri" in field.baseName:
&amp;nbsp;&amp;nbsp;&amp;nbsp; if field.baseName != "Priority":
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; wrongname = field
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddWarning('The priority field was named ' + str(field.baseName) + " and will be renamed.")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddField_management(preparedfeatures, "Priority", "SHORT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management(preparedfeatures, "Priority", field, "PYTHON_9.3", "")
&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage('%s had the proper fields. There must be some other error.' % preparedfeatures_str)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddWarning(arcpy.GetMessages())
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The new field is all 0's rather than the contents of the original 'Priority_1' field.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thoughts?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 02:56:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/changing-field-names-in-python-script/m-p/633090#M49330</guid>
      <dc:creator>CharliePeterson</dc:creator>
      <dc:date>2021-12-12T02:56:14Z</dc:date>
    </item>
    <item>
      <title>Re: Changing field names in python script</title>
      <link>https://community.esri.com/t5/python-questions/changing-field-names-in-python-script/m-p/633091#M49331</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I didn't really review the calculate field parameters before writing my response earlier&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.CalculateField_management(preparedfeatures, "Priority", "!" + field.baseName + "!", "PYTHON_9.3", "")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;should be used, as the 3rd parameter needs to be an SQL expression for the field name, not the field name itself.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm not sure about actually changing the field name with python, you'd at minimum would have to open an editting session and even then you probably could only edit the Alias Name, not the Base Name.&amp;nbsp; I could be completely wrong about that, someone please correct me if I am.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Hope this helps&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 13 May 2014 19:54:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/changing-field-names-in-python-script/m-p/633091#M49331</guid>
      <dc:creator>IanMurray</dc:creator>
      <dc:date>2014-05-13T19:54:35Z</dc:date>
    </item>
    <item>
      <title>Re: Changing field names in python script</title>
      <link>https://community.esri.com/t5/python-questions/changing-field-names-in-python-script/m-p/633092#M49332</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Awesome. The following worked just as intended.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;oldfield = str(field.baseName)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;'(!' + oldfield + '!)'&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 13 May 2014 20:00:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/changing-field-names-in-python-script/m-p/633092#M49332</guid>
      <dc:creator>CharliePeterson</dc:creator>
      <dc:date>2014-05-13T20:00:49Z</dc:date>
    </item>
    <item>
      <title>Re: Changing field names in python script</title>
      <link>https://community.esri.com/t5/python-questions/changing-field-names-in-python-script/m-p/633093#M49333</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;I want to:&lt;BR /&gt;* Read the list of fields in a shapefile&lt;BR /&gt;* check for a field starting with "Pri*"&lt;BR /&gt;* If it is not named "Priority", I want to rename them to "Priority"&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;A far more efficient way to complete this operation is to use the Merge_management tool with a field map to make a new copy of your shapefile with the field names and order just the way you want them.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The FieldMappings and FieldMap objects are a little tricky but not too difficult once you figure them out.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 13 May 2014 22:03:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/changing-field-names-in-python-script/m-p/633093#M49333</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2014-05-13T22:03:11Z</dc:date>
    </item>
    <item>
      <title>Re: Changing field names in python script</title>
      <link>https://community.esri.com/t5/python-questions/changing-field-names-in-python-script/m-p/633094#M49334</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;A far more efficient way to complete this operation is to use the Merge_management tool with a field map to make a new copy of your shapefile with the field names and order just the way you want them.&lt;BR /&gt;&lt;BR /&gt;The FieldMappings and FieldMap objects are a little tricky but not too difficult once you figure them out.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Is this truly the more efficient way? (To note: I don't care about the order at all. I simply need a field called "Priority" so I can use it as a case field in Summary Stats.)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Additionally, part of my code that I didn't include in the original post was an iterator that goes through &amp;gt;1000 shapefiles.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;It seems wasteful to create all new shapefiles just to rename 1 field.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;EDIT: I just re-read your post and it clicked that you meant create one large shapefile (merge).&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Although I still see the size of the file (merging &amp;gt;1000 shapefiles with 10-100+ features each) as a potential problem since I don't know all the possibilities for the name of the "pri*" field.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;And, finally, field mapping seems like it would be tricky if I don't know the exactly field I want to map.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thoughts?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 14 May 2014 01:15:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/changing-field-names-in-python-script/m-p/633094#M49334</guid>
      <dc:creator>CharliePeterson</dc:creator>
      <dc:date>2014-05-14T01:15:44Z</dc:date>
    </item>
  </channel>
</rss>

