Change field names with Python - new question

7060
17
02-15-2016 07:02 PM
JKBellamkonda
New Contributor III
# Import system modules
print "Importing modules"
import arcpy
import os

# Check out extensions and Overwrite outputs
print "Checking for extensions and Overwrite outputs"
arcpy.CheckOutExtension("Analysis")
arcpy.CheckOutExtension("Data Management")
arcpy.env.overwriteOutput = True

# Input Parameters
print "checking input Parameters"
infeature = arcpy.GetParameterAsText(0)
delete_fields = arcpy.GetParameterAsText(1)

# Delete unwanted fields
print "Deleting unwanted fields"
arcpy.DeleteField_management (infeature, [delete_fields])

# Alter field alias by capitalising the initial letter of the word.
print "Changing the field alias by turning 1st letter of the word to uppercase"
fields = arcpy.ListFields(infeature)
for field in fields:
   fname = field.name
   alias = fname.title()
   arcpy.AlterField_management (infeature, field, alias)

print "done"

Hi Sephe Fox,

I have been trying to dig into this code to alter it to my specifications, but seems like I am lost. could you help me out with this please. Trying to create a script tool that allows you to delete fields and alter alias names.

Branched to new question by: Xander Bakker From original thread: https://community.esri.com/thread/151288

17 Replies
XanderBakker
Esri Esteemed Contributor

Hi JK Bellamkonda , I branched you question to a new thread.

I noticed a few things that will generate problems in the script you posted.

  • You check out the "extensions" Analysis and Data Management, on lines 8 and 9, but these are toolboxes and not extensions and can be used without loading or checking out anything.
  • You read on line 15 the list of fields as a string. Normally a list is parsed as a string separated by a semicolon. You should split that string by the semicolon to be able to get a list that can be used on line 19 to delete a list of fields. For instance: lst_fields = delete_fields.split(';')
  • On line 27 you specify a field object to identify the field to be altered. The documentation of this function has some errors. You should specify the field name (field.name) instead.

I will add feedback to the Help page to have that fixed.

JKBellamkonda
New Contributor III

Hi Xander Bakker's Blog thanks for your response. As I mentioned earlier I am trying to convert this in to a tool, regarding line 15 I have set the parameters for the tool to delete only selected fields as shown below. only thing I am struggling with is line 27 to loop over the rest of the fields and change alias. Thanks to Luke Sturtevant​ for mentioning issue with Alias case change.

0 Kudos
LukeSturtevant
Occasional Contributor III

Also,  the alias name does not seem to recognize a case change. So if you are attempting to change just the first letter to uppercase and it does not appear to be successful you may want to look at the workaround here.

RebeccaStrauch__GISP
MVP Esteemed Contributor

I'm testing out the OTB tool Alter Field—Help | ArcGIS for Desktop which in thoey

Rename fields and field aliases, or alter field properties

I think this actually does this by adding a new field, deleting the old.  Unfortunately, although it lists being able to change the length, etc., it doesn't seem to be able to do this unless the FC is empty...which is a bummer in my case (need to just append into a new structure I guess).

Anyway, is this what you are trying to do?

0 Kudos
JKBellamkonda
New Contributor III

Hi Rebecca Strauch, GISP​ I am only trying to change the field alias. its such a task to create new fields, copy and delete the old ones.

0 Kudos
RebeccaStrauch__GISP
MVP Esteemed Contributor

the help says...

"This tool provides the ability to rename fields or rename field aliases for any geodatabase table or feature class."

and the renaming the field is optional.  I just did it, and although I had to removed the FC from my TOC in ArcMap, when I added it back, the alias was changed.  If you also change the fieldname, it is moved to the end of the list.  But changing the alias only left it in the same order.

After reopening the file (an f5 refresh didn't seem to be enough)

my snippet after running...

# Replace a layer/table view name with a path to a dataset (which can be a layer file) or create the layer/table view within the script
# The following inputs are layers or table views: "tmppt1final"
arcpy.AlterField_management(in_table="tmppt1final", field="DDMLat_12", new_field_name="", new_field_alias="blah", field_type="TEXT", field_length="255", field_is_nullable="NULLABLE", clear_field_alias="false")

It should be possible to create a list of the [in_table, field, new_field_alias]  and loop thru them to have them updated.  I did the above manually, but about to put this to use in my script (but I'm going to rename the duplicate names, in my case).

edit: btw - took  0.08 seconds to run the command.

0 Kudos
JKBellamkonda
New Contributor III

Thanks Rebecca Strauch, GISP​ . I dont really want to hard code the field names as I will be using it as a script tool . Can you guide me how to use the existing field names in my code( keeping in mind that my code also deletes few fields prior to Alter field.

0 Kudos
RebeccaStrauch__GISP
MVP Esteemed Contributor

I'm doing something similar right now in my script.  I have (from previous processing) three lists that include coordinates that I am creating three point FC, and getting coordinates in XY-projection, and Geographic coord in DD, DDM and DMS.....(using Project, addxy, and Convert Coordinate Notation—Help | ArcGIS for Desktop) then merging the three files with JoinField.  really messy if I let ArcGIS autoname the fields, since they are the same in each.  So I'm going to alter the field names to include "pt1", "pt2", etc, and change the length of the auto TEXT fields from 255 to 15.  As I mentioned, just starting to work on this.  But I am also deleting fields.

Some tools that may help you...(Look at the code samples)

ListFields—Help | ArcGIS for Desktop

Fyi..my input starts like

pt1List =  [[104, 259572.28396514762, 1569679.2337295527, u'Contour', 2333, 711, 3338], [102, 307382.1354489387, 1540435.947750746, u'Contour', 5738, 1749, 3338], [103, 318046.4257102255, 1572115.318007391, u'Contour', 5646, 1721, 3338]]
pt2List = [[104, 261920.13532314647, 1571191.8980065093], [102, 310325.1529914262, 1538520.823783757], [103, 318832.6403342534, 1572402.314232631]]
pt3List = [[104, 262729.2297764599, 1574104.0470867334], [102, 311161.3833612903, 1542535.1787147326], [103, 320759.5157311091, 1571095.6504666873]]

and my current output is....

>>> outFCdms = 'tmppt1final'
... aaa = arcpy.ListFields(outFCdms)
... for i in aaa:
...   print(i.name, i.type, i.length)
(u'OID', u'OID', 4)
(u'Shape', u'Geometry', 0)
(u'PtID', u'SmallInteger', 2)
(u'TransType', u'String', 25)
(u'elev_ft', u'SmallInteger', 2)
(u'elev_m', u'SmallInteger', 2)
(u'xyProjID', u'SmallInteger', 2)
(u'x', u'Double', 8)
(u'y', u'Double', 8)
(u'ddlat', u'Double', 8)
(u'ddlong', u'Double', 8)
(u'POINT_X', u'Double', 8)
(u'POINT_Y', u'Double', 8)
(u'DDLat_1', u'Double', 8)
(u'DDLon', u'Double', 8)
(u'DDMLat', u'String', 255)
(u'DDMLon', u'String', 255)
(u'DMSLat', u'String', 255)
(u'DMSLon', u'String', 255)
(u'ddlat_12', u'Double', 8)   # etc.

And this is something that will be run numerous times....so I will need to script it too....but for me, I'll do it at the FC before my jooining the 3 tables.  This is probably not exactly what you are doing, but probably similar.  If you give an example of where you are starting and what you would like in the end, it would be easier for others to help.  I can share what I come up with too.

JKBellamkonda
New Contributor III

So this is how my FC is now as shown below . I want to delete Join_Count, TARGET_FID, JOIN_FID and change the "appellation to Appellation" "affected_surveys to Affected_Surveys" and similar with the other fields 

0 Kudos