Python Problem

3304
11
12-02-2014 02:34 PM
DavidLeslie
New Contributor II

I am trying to write a script that that changes the aliases of a table's fields, but when I ran it I got this code error:

ExecuteError: ERROR 001601: Failed to alter field alias.

Failed to execute (AlterField).

This makes no sense to me and I would love any input as to why this happened.

Also, here is my code that generated this error:

# Import geoprocessing
import arcpy


# Set workspace
arcpy.env.workspace = r'U:\newberry\new.gdb'


# Creates a list of fields from the Metadata_2010 table
fieldListTable = arcpy.ListFields("METADATA_2010")


# Creates a cursor - a data access object
rows = arcpy.SearchCursor("METADATA_2010")


# Creates an empty table to store row values
fullNames = []


# For loop thats iterates through the rows in
# the Full_Name field of the Metadata_2010 table
for row in rows:
    # Adds the row value to the fullNames list
    fullNames.append(row.getValue(fieldListTable[2].name))


# Creates a sliced list of fields from the census block group
# polygon feature class
fieldList = arcpy.ListFields("please")[2:630]


# For loop thats changes the existing alias name in the table
# to the name in the fullNames list
for i in range(len(fieldList)):
    arcpy.AlterField_management('please', str(fieldList.name),'',str(fullNames))
Tags (2)
0 Kudos
11 Replies
XanderBakker
Esri Esteemed Contributor

The actual Help page doesn't hep much... it just states that the alias is invalid and you should try a different alias. ArcGIS Help (10.2, 10.2.1, and 10.2.2)

Did you print the str(fieldList.name) and str(fullNames) to see what it's trying to do? And you are using an empty string as the optional output field name. Are you sure that 's OK?

Is it the first field where the error occurs or did it change some fields?

0 Kudos
DavidLeslie
New Contributor II

I'm not sure about the empty string, but when I ran it without one it crashed immediately. Perhaps you would know the proper way  to skip over an optional field?

The program did go about halfway through the renaming of aliases process and then stopped at this string:

SEX BY EDUCATIONAL ATTAINMENT FOR THE POPULATION 25 YEARS AND OVER - Universe:  Population 25 years and over - Total:  Male:  Doctorate degree -- (Estimate).

This is interesting because the ones proceeding it are very similar. Any other suggestions for this problem would be great and thanks for your help!

0 Kudos
Zeke
by
Regular Contributor III

As an aside, Curtis Price wrote a nice primer on how to post code on the Geonet forums. Makes it much easier to read.

Posting Code blocks in the new GeoNet

0 Kudos
Zeke
by
Regular Contributor III
# Import geoprocessing
import arcpy
# Set workspace
arcpy.env.workspace = r'U:\newberry\new.gdb'

# Creates a list of fields from the Metadata_2010 table
fieldListTable = arcpy.ListFields("METADATA_2010")
  
# Creates a cursor - a data access object
rows = arcpy.SearchCursor("METADATA_2010")
  
# Creates an empty table to store row values
fullNames = []
  
# For loop thats iterates through the rows in
# the Full_Name field of the Metadata_2010 table
for row in rows:
    # Adds the row value to the fullNames list
    fullNames.append(row.getValue(fieldListTable[2].name))
  
# Creates a sliced list of fields from the census block group
# polygon feature class
fieldList = arcpy.ListFields("please")[2:630]
  
# For loop thats changes the existing alias name in the table
# to the name in the fullNames list
for i in range(len(fieldList)):
    try:
        arcpy.AlterField_management('please', str(fieldList.name),'',str(fullNames))
    except Exception as e:
        print('FieldList name is: {0}\n
               fullName is: {1}\n
               Error: {2}'.format(fieldList.name, fullNames, e.message)

You might want to consider using an arcpy.da.SearchCursor, much faster.

I added a try statement to catch the error and show the problematic values. I think the root of the problem may be in the code building your lists, but am not sure, having a hard time working out the details.

0 Kudos
DavidLeslie
New Contributor II

Upon further inspection of my census block group data (which includes: a feature class (polygon) and a metadata table) I found that the feature class had an additional field, OBJECTID_1, that was not included in the metadata table. After I deleted that field from the feature class, the program ran just fine except for an out or range error, which i'm assuming has to do with the use of len(fieldList) in the for loop. I really appreciate the quick responses.

0 Kudos
Zeke
by
Regular Contributor III

Are you working with the 5 year ACS estimates? Working on that myself right now.

0 Kudos
DavidLeslie
New Contributor II

Yes I am.

0 Kudos
Zeke
by
Regular Contributor III

Those field names aren't helpful, are they?

0 Kudos
Zeke
by
Regular Contributor III

If you don't mind, where did you get the field names for the data? The metadata file I have has short name (B23003e1) with a text description which can be very long. I've been using the description as a field name, but they get truncated when I pull them into a gdb table.

0 Kudos