1

I'm a novice programmer and I would like to ask for something which will facilitate my work. I need to rename all the field names in some shapefiles I am working on and since renaming is an arduous task, I need to automate using some scripting. Renaming a shapefile field name is not possible so I used geodatabase. Now, the 'Alter Field' tool is limited to only one field name at a time.

Can somebody help me with the script I need?

Below is the example.

Example:

arcpy.AlterField_management(r'D:\Workspace\11-30-2014\Approved GDB\Nisma_GIS DATA\2NISMA_WETCO.gdb\NISMA_WETCO\CHAMBER2', 'LOCATION_I', 'LOCATION_ID', 'LOCATION_ID')

How can I include these: ('X_COORD', 'X_COORDINATE', 'X_COORDINATE', 'Y_COORD' , 'Y_COORDINATE', 'Y_COORDINATE'...etc)

I tried just putting all in the same code but I got this error:

TypeError: AlterField() takes at most 4 arguments

I'm using ArcGIS 10.2.2, Standard (ArcEditor) License.

closed as off-topic by PolyGeo Sep 22 at 21:02

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "When seeking help to debug/write/improve code always provide the desired behavior, a specific problem/error and the shortest code (as formatted text, not pictures) needed to reproduce it in the question body. Providing a clear problem statement and a code attempt helps others to help you." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

3

you'll need to loop, so you first need to create the list of your old and new fields

oldfields = ['XCOORD', 'YCOORD']
newfields = ['X_COORDINATE', 'Y_COORDINATE']

for i in range(len(oldfields)):
    arcpy.AlterField_management(r'D:\Workspace\11-30-2014\Approved GDB\Nisma_GIS DATA\2NISMA_WETCO.gdb\NISMA_WETCO\CHAMBER2', oldfields[i], newfields[i], newfields[i])

Note that the alias is optional, and that you could use a third list with the aliases if you want your aliases to be different from the field names.

2

Use a FieldInfo object.

# create the object
fieldInfo = arcpy.FieldInfo()
# add field to the filedinfo object
fieldInfo.AddField("OldName", "NewName", "VISIBLE", "NONE") # takes 4 args
# can do this for any number of fields, all added to the fieldInfo object.
# then use Make table or featureclass view, using the fieldInfo object.
# then copy out

see here : FieldInfo help

I used this script to reformat a whole bunch of tables in a mdb. The header had multiple space, % & * in them. AlterField couldn't handle this but using FieldInfo worked.

tbls = arcpy.ListTables()

for tbl in tbls:
    t1 = time.time()
    tblValid = arcpy.ValidateTableName(tbl)
    outTbl = os.path.join(HomeDir, Outdb, tblValid)
    fieldInfo = arcpy.FieldInfo()
    # print "Old {}  New {}".format(tbl, tblValid)
    fNames = [f.name for f in arcpy.ListFields(tbl)]
    for f in fNames:
        fTemp = f.strip()
        if "%" in fTemp:
            fTemp = fTemp.replace("%", "_pc")
        if "*" in fTemp:
            fTemp = fTemp.replace("*", "")
        if " " in fTemp:
            fTemp = fTemp.replace(" ", "")
        if "__" in fTemp:
            fTemp - fTemp.replace("__", "_")
        fValid = arcpy.ValidateFieldName(fTemp)
        # print "{}  {}".format(fTemp, fValid)
        fieldInfo.addField(f, fValid, "VISIBLE", "NONE")
    arcpy.MakeTableView_management(tbl, tbl + "_vw", "", "", fieldInfo)
    if arcpy.Exists(outTbl):
        arcpy.Delete_management(outTbl)
    arcpy.CopyRows_management(tbl + "_vw", outTbl)

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