Hi
I have an attribute table with many fields where I have to change the field name. Most of the field names start with All_txt_ .
As I found out in the Data Management Forum, there is no way to automatically change all these field names in ArcGIS with a tool. Can someone tell me on how to do so with python? (I am not familiar with Python)
I already saw this page: Mapping input fields to output fields—Help | ArcGIS for Desktop
However, it does not really help me further as I don't know Python so well..
Best, P.
*Question is post-crossed on GIS Stack Exchange Forum
Solved! Go to Solution.
Here's another possibility, that doesn't use the Alter Fields tool. Please don't try it without making a copy of your data first.
#Import geoprocessing
import arcpy
#Set workspace
arcpy.env.workspace = r'your workspace'
fc = 'your feature class'
fieldList = arcpy.ListFields(fc) #get a list of fields for each feature class
for field in fields:
# do not duplicate oid and geometry fields
if field.Type == "OID" or field.Type == "Geometry":
continue
# Split field name at _ symbol
fieldNames = field.name.split("_")
del(fieldNames[0:1])
# add a new field using the same properties as the original field
arcpy.AddField_management(fc, fieldNames[0], field.Type)
# calculate the values of the new field
# set it to be equal to the old field
arcpy.CalculateField_management(fc, fieldNames[0], field)
# delete the old fields
arcpy.DeleteField_management(fc, field)
You can use the alter field tool either in a loop using a python dictionary or more simply create a bunch of one liners using something like excel to save typing:
e.g.
arcpy.AlterField_management(r'C:\Data\f.gdb\foodlyr', 'All_txt_ws10melon', 'ws10melon'),
arcpy.AlterField_management(r'C:\Data\f.gdb\foodlyr', 'All_txt_ws10tomato', 'wstomato'),
and then run that by pasting it into your python command line
now, if you really have a lot, a python script with a loop id the better way to go.
Thank you,
As I have many fields like this, a loop would make more sense.
Can you tell me how to do this? (I use ArcGIS 10.1 - as alter field management is for 10.2 onwards I am not sure if this will work)
Peter, this is a run-on question from another thread. My python skills are very rudimentary, but what he needs to do is loop though his fields, and if they have All_text_ at the beginning of the name, strip it off, and make that the new field name.
This is my poor attempt, but I'm sure it's not quite right:
#Import geoprocessing
import arcpy
#Set workspace
arcpy.env.workspace = r'your workspace'
fc = 'your feature class'
fieldList = arcpy.ListFields(fc) #get a list of fields for each feature class
for field in fieldList: #loop through each field
fname = field.name
if fname.startswith("All_txt_")
: #look for the stated text
new_name = fname.replace(
"
All_txt_","") #replace with new field name
arcpy.AlterField_management(fc, field, new_name)
Here's another possibility, that doesn't use the Alter Fields tool. Please don't try it without making a copy of your data first.
#Import geoprocessing
import arcpy
#Set workspace
arcpy.env.workspace = r'your workspace'
fc = 'your feature class'
fieldList = arcpy.ListFields(fc) #get a list of fields for each feature class
for field in fields:
# do not duplicate oid and geometry fields
if field.Type == "OID" or field.Type == "Geometry":
continue
# Split field name at _ symbol
fieldNames = field.name.split("_")
del(fieldNames[0:1])
# add a new field using the same properties as the original field
arcpy.AddField_management(fc, fieldNames[0], field.Type)
# calculate the values of the new field
# set it to be equal to the old field
arcpy.CalculateField_management(fc, fieldNames[0], field)
# delete the old fields
arcpy.DeleteField_management(fc, field)
I wish someone would pipe up who is a python expert. I'm flailing when it comes to python, but I really want to get PV the help he needs. Dan Patterson? Joshua Bixby?
Xander Bakker ? Robert Scheitlin ?
Sorry, I'm of no use on this one. I program mostly in vb.net...
I think that the "AlterField_management" example provided by Sephe Fox is probably the best way to go. For older versions of ArcGIS you could go with the Add Field, Calculate Field and Delete Field, but if you have access to the Alter Field tool, you can better use that one.
The code provided by Sephe looks OK to me, but could be written a little shorter, assuming that they are all text fields and the "ws10" can also be omitted:
import arcpy
fc = r'path to your feature class'
flds = arcpy.ListFields(fc, field_type="String")
for fld in flds:
if fld.name.startswith("All_txt_ws10"):
arcpy.AlterField_management(fc, fld.name, fld.name.replace("All_txt_ws10",""), fld.name.replace("All_txt_ws10",""))
Note: although the help states that the input field should be of type field, it actually only works if you provide the input field name.
I'll report this to Esri.
Edit 1: just reported to Esri
Edit 2: added the alias, since otherwise the old name is still shown to the user
I don't think I can answer your question the way a python guru could. But maybe if you try model builder and export to python script? Using the alter field geoprocessing tool in model builder and then exporting it as a python script?
Have you seen this? arcgis 10.0 - Renaming field using ArcPy? - Geographic Information Systems Stack Exchange
The OP went shopping elsewhere....
python - Change field names automatically - Geographic Information Systems Stack Exchange