I am trying to add multiple fields to an attribute table using the same attribute's tables current field names. I want the new fields to be text fields that can be populated with either yes or no later on. When I run the script below I get the error above. The script works halfway through and crashes. Any help or suggestions are welcome.
import arcpy
from arcpy import env
# To allow overwriting the outputs change the overwrite option to true.
arcpy.env.overwriteOutput = True
# ###############################################################################
# [1] Script adds duplicate (text) fields to attribute table with shortened name
# Allows for Yes and No values to be added to table
# Identify shapefile
blocks = r"C:\Users\Workspace\scrap\FB_CFDReport.shp"
# Generate list for each field in blocks
fields = arcpy.ListFields(blocks)
# For loop creates duplicate text fields in block shapefile
for field in fields:
field4 = field.name
f4 = field4[0:8] + "X"
print(f4)
arcpy.AddField_management(blocks, f4, "TEXT")
#arcpy.SelectLayerByAttribute_management(blocks, selection_type="NEW_SELECTION", where_clause= field+ " > 0", invert_where_clause="")
#arcpy.CalculateField_management(blocks, f4, 'Yes')
#arcpy.SelectLayerByAttribute_management(blocks, selection_type="NEW_SELECTION", where_clause= field+" = 0", invert_where_clause="")
#arcpy.CalculateField_management(blocks, f4, 'No')
/blogs/dan_patterson/2016/08/14/script-formatting would help with line numbers
You might want to check the field names to make sure. Maybe show the results of your print statements.
Was there an error? or didn't it work, it isn't clear from the output
Thanks for the response and see below for the part of the print statement and error message.
I have changed most of the field names multiple times because I thought it might be something related to that. I'm going to try change the field names to a coded system to see if that works..
>>> FIDX
ShapeX
BLOCK10_X
CDFWX
Row_LabeX
anchovy_X
BarracudX
bs_giantX
bs_kelpX
blacksmiX
bonnito_X
pompano_X
cabezonX
cod_pacX
croak_unX
croak_whX
croak_yeX
dolphin_X
Ca_morraX
eel_wolfX
escolarX
flnd_arrX
flnd_staX
flyigfisX
gby_yfinX...(I deleted approx 50 entries)
...blue_shaX
Traceback (most recent call last):
File "C:\Python27\ArcGIS10.6\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 326, in RunScript
exec codeObject in __main__.__dict__
File "C:\Users\Documents\GIS_Projects\Scripts\AddFieldIterator.py", line 25, in <module>
arcpy.AddField_management(blocks, f4, "TEXT")
File "C:\Program Files (x86)\ArcGIS\Desktop10.6\ArcPy\arcpy\management.py", line 3435, in AddField
raise e
ExecuteError: ERROR 999999: Error executing function.
Failed to execute (AddField).
any of the entries begin with a number or did the first 8 characters have a space or some other non-underscore character?
The line where the error message failed would have been useful.
Also, you have ruled out there being more than 255 fields
While you are at it, you might want to review
Geoprocessing considerations for shapefile output—Appendices | Documentation
And I would suggest moving the testing to a featureclass in a file geodatabase
Oh man, I can't believe I forgot about the 255 field limit. That has cleared up my issues with the code. Thanks Dan, I really appreciated your help with this issue.
It is always something