Change field names with Python - new question

8374
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
RebeccaStrauch__GISP
MVP Emeritus

try something like this:

fc = 'tmppt1final'
dropFields = "DDlat_1; DDLon"      # removed [ ]  one test wanted them...next one didn't
alterFields = [["elev_ft", "Elev_FT"], ["elev_m", "Elev_M"], ["x", "X"], ["y", "Y"]]
arcpy.DeleteField_management(fc, dropFields)
for old, new in alterFields:
  tmpName = ("{0}_tmp".format(old))
  arcpy.AlterField_management(fc, old, tmpName)
  arcpy.AlterField_management(fc, tmpName, new)

changing you "fc" and the dropFields (a list of fields, separated by a semi-colon) and AlterFields (oldname and new name, for each field)

I changed the field name....but you could adjust it for the alias.  As mentioned earlier, if only changing alias, the fields seem to stay in the same order....changing the name puts the field at the end.  So, you would need to update each field in order to get them back (I don't think there is a change field order command...but may need to look again for my purposes).

There are ways you could probably automate the new name with the first letter of a word always in Caps, etc....and skipping some fields you don't want that to happen on.  There are other threads that talk about things like that, and Dan Patterson​ might have somethin in his blogs already....and he has magical things happening with lists, etc...but I tend to try to keep things simple for me in the future....I'm a good hack...not hacker.  Couple others I can think of to tag here are Xander BakkerDarren Wiens​   

Edit:  updated my code above to remove the  [ ] on the dropFields list.  For some reason my machine wanted them when testing....but once I had it in my script, it didn't  go figure.

DanPatterson_Retired
MVP Emeritus

Rebecca, try  .... title  ... others include upper, lower etc

>>> a = "elev_ft"
>>> a.title()
'Elev_Ft'
RebeccaStrauch__GISP
MVP Emeritus

It's not what I need right now, but that would probably work for JK's needs.  Testing on a list, and printing...

fc = 'tmppt1final' 

alterFields = ["elev_ft", "elev_m", "x", "y"] 

for old in alterFields:

  tmpName = ("{0}_tmp".format(old))

  new = old.title()

  print("old: {0}, tmp: {1}, new: {2}".format(old,tmpName,new))

Thanks Dan....that might come in handy for me some day.

DavidWasserman
Occasional Contributor III

Hi Jk,

I have a batch rename alias field tool that might be helpful as a template.
Batch Rename Field Alias

I use a text string that is split so it can do them in batch. Hope this is helpful.
David

David Wasserman, AICP
JKBellamkonda
New Contributor III

Hi David Wasserman's Blog

Thanks for the tool. It was really helpful but I am trying to automate the whole process so that I don't have to spend time typing all the alias names.

0 Kudos
DavidWasserman
Occasional Contributor III

Hi JK,

I understand! I specifically designed this one just to deal with data that was processed a lot or data sets whose fields don't change (so the same tool run could be used over and over for templated data). Just thought it might be a useful template for a final tool etc.

In the case of making all the aliases title case, you could modify my script to instead take a list of field names, then cast them as a string (str(unicode) just in case), then use the title method mentioned earlier in the thread. It seems like you are on the right track regardless.

David

David Wasserman, AICP
JKBellamkonda
New Contributor III

Finally managed to create a tool that does what I wanted, thanks to Rebecca Strauch, GISP, David Wasserman's Blog  and Luke Sturtevant for your guidance.

# Import system modules
import arcpy
import os

# Check out extensions and Overwrite outputs
arcpy.env.overwriteOutput = True

# Input Parameters
infeature = arcpy.GetParameterAsText(0)

# List all the field names of infeature.
fields = arcpy.ListFields(infeature)

# Change the field alias inital letters of the word to uppercase.
print "Changing the field alias to field name by Capitalising the initial letter"
for field in fields:
         if not field.required:
           # change field alias to a temporary name to recognise change by stripping of last 2 charecters
           arcpy.AlterField_management (infeature, field.name, field.name, field.name.rstrip(field.name[-2:]))


           #Change the field alias back to original field name without underscores and turn the initial letter of the word to uppercase
           arcpy.AlterField_management (infeature, field.name, field.name, field.name.replace("_"," ").title())

            print "Script Completed Successfully"
DavidWasserman
Occasional Contributor III

Thanks for sharing this. I might want to modify my tools based on some of the ideas here! 😃

David Wasserman, AICP
0 Kudos