2

I can't get my code to run correctly and I'm sure have the logic wrong. I have two lists declared as parameters: one is a list of field names that I would like to change from and the other is a list of field names I would like to change to.

I can get the python to run correctly if I first check to ensure the table has all of the old field names and does not have any of the new street names. But, what I would like to do is automate that 'check' - so that the script first checks to see if the old field name exists and, if so, continues with the AlterField task. If the old field names does not exist, it skips that particular change and continues on to the next one.

Here is the (bad) code that I have so far:

import arcpy
arcpy.env.workspace = r'C:\data\MyGDB.gdb\Addresses_WGS84'

fcTable = 'Copy_of_roads_from_sde'
oldfields = ['STR_NAME1', 'SPEED_MPH', 'LZIP', 'RZIP' ]
newfields = ['ST_CONCAT', 'SPEED_IMP', 'ZIP_L', 'ZIP_R']

for field in oldfields:
    if field.name in arcpy.ListFields(fcTable):
        arcpy.AlterField_management(fcTable, oldfields, newfields)

I've tried a few different approaches, stumbling around trying to use break, elif, and else statements in various places, but to no avail. The error I keep getting (when I actually get the syntax right) is:

Failed to execute. Parameters are not valid. 
ERROR 000728: Field STR_NAME1 does not exist within table 
Failed to execute (AlterField).

I'm sure that I have the overall, big-picture concept wrong. Also, FYI: my code continues on, after altering these field names, to use AddField_managment to add a bunch of other, new fields to my data.

  • You're right - the [i] wasn't supposed to be in there (was a remnant from when I was trying to run the AlterField itself as a loop). I've edited my question: I still get the same error when executing. – Matt Goodman Jun 30 '17 at 15:00
  • To compliment BERA's answer you should look at the help file for this tool and look at the Syntax section. This explains exactly what the inputs and outputs of the tool are. So the second parameter is a field the third is the new name. Look at Data Type, where does it say a list of strings which is what you were trying to input? It does not! This is what the help file is explaining and why you should be looking at it. So rather than second guess what you think it should take, look at the help and see exactly what the tool wants. – Hornbydd Jun 30 '17 at 16:31
3

You are only iterating over the field names in the oldfield list. Also you are passing the complete lists to alter field, not individual field names.

I think you should use a dictionary instead of trying to iterate over two lists at the same time.

import arcpy
arcpy.env.workspace = r'C:\data\MyGDB.gdb\Addresses_WGS84'
fcTable = 'Copy_of_roads_from_sde'

d={'STR_NAME1':'ST_CONCAT','SPEED_MPH':'SPEED_IMP', 'LZIP':'ZIP_L', 'RZIP':'ZIP_R'}

for field in arcpy.ListFields(fcTable):
    if field.name in d:
        arcpy.AlterField_management(fcTable, field.name, d[field.name])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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