Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Guys and gals could you please provide me with a sample code as to how to rename a field name in a Feature Layer or Table. I've been trying to use the FieldInfo object to do so over the last couple of days but it's not happening for me.

share|improve this question
    
What software are you using? –  lynxlynxlynx Jan 17 '13 at 21:42
    
arcgis and I attempting to use FieldInfo via arcpy. –  lemuel Jan 17 '13 at 21:48
    
Could you post a Python code snippet illustrating where you are stuck, please? Are you trying to rename in an underlying feature class or table; or to rename in the corresponding feature layer or table view? –  PolyGeo Jan 17 '13 at 22:58
    
@ PolyGeo I am attempting to use this string 'fieldinfo.setFieldName(0,'FID')' in a for loop in order to simply rename an existing field name via Python. –  lemuel Jan 18 '13 at 19:07
add comment

2 Answers

Sorry for the delay. I just modified the python script and it is now using the "setNewName" method to rename a existing field instead of adding a new field.

# Import system modules
import arcpy
import os
from arcpy import env
env.overwriteOutput = True

inputFeatureClass=r"C:/Desktop/Cities/Cities.gdb/USA_Cities"
layer = "temp_layer"

arcpy.MakeFeatureLayer_management(inputFeatureClass, layer)
desc = arcpy.Describe(layer)

# If a feature layer, continue
if desc.dataType == "FeatureLayer":
    fieldInfo = desc.fieldInfo
    index = 0
    # Use the count property to iterate through all the fields    
    while index < fieldInfo.count:
        if fieldInfo.getFieldName(index)=="NAME_1":
            fieldInfo.setNewName(index, "NAME_CITY")

        elif fieldInfo.getFieldName(index)=="RegionNames":
            fieldInfo.setNewName(index, "NAME_REGION")
        index += 1

arcpy.MakeFeatureLayer_management("temp_layer","cities_lyr", "", "", fieldInfo)
arcpy.CopyFeatures_management("cities_lyr", "C:/Desktop/Cities/Cities.gdb/City_Renamed")

I think this is what you are looking for.

Please let me know if you have any questions regarding the code above.

Thanks,

Jinnan

share|improve this answer
    
I've tried you modified code but it does not work work for me. Your code looks right and as a matter of fact when I run it I do not get an error from the IDE. I will continue to try you method, I sure the lies on my end. –  lemuel Jan 19 '13 at 5:55
add comment

What this example code does is to add two NEW fields ("NAME_CITY"; "NAME_REGION") into your fieldinfo with desired new field names instead of just renaming the ORIGINAL field ("NAME_1"; "RegionNames").

After you renamed your fields you need to copy your table view/feature layer into a new table/feature class and that will be your output with renamed fields in it.

import arcpy
import os
from arcpy import env
env.overwriteOutput = True

inputFeatureClass=r"C:/Desktop/Cities/Cities.gdb/USA_Cities"

#get fields from your input feature class
fields= arcpy.ListFields(inputFeatureClass)

# Create a fieldinfo objects
fieldinfo = arcpy.FieldInfo()

# Iterate through the fields and set them to fieldinfo
for field in fields:
    if field.name == "NAME_1":
        fieldinfo.addField(field.name, "NAME_CITY", "VISIBLE", "")
    elif field.name == "RegionNames": 
        fieldinfo.addField(field.name, "NAME_REGION", "VISIBLE", "")

# The created table view will have fields as set in fieldinfo objects
arcpy.MakeTableView_management(inputFeatureClass, "temp_Table_View", "", "", fieldinfo)

# Write the table view to a new table
arcpy.CopyRows_management("temp_Table_View", "C:/Desktop/Cities/Cities.gdb/City_Table")

Hope this is helpful for you.

Thanks,

Jinnan

share|improve this answer
    
instead of using fieldinfo.addField() should I use fieldinfo.setFieldName (index, field_name) to rename the field I'd like or should I use fieldinfo.setNewName? –  lemuel Jan 18 '13 at 3:47
    
Instead of adding a field I'd rather just rename it by using the methond in the comment above this one. –  lemuel Jan 18 '13 at 19:57
    
Just posted another answer which should give you what you want, thanks. –  Vanni Zhang Jan 18 '13 at 20:22
    
I tried this code for renaming feature class field name, but it did not work for me. Can you give me any other pointer? –  Emi Feb 20 '13 at 3:01
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.