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
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.