Select to view content in your preferred language

AddField Failed to Execute???

5394
11
04-30-2013 12:59 PM
MichelleCouden1
Deactivated User
I am trying to join 2 fields in one layer which is a shapefile and join 2 fields in another layer which is a gdb. I think I have the coding right but it is not wanting to add my join field in my shapefile. It gives me a failed to execute AddField on the first one which is the shapefile. Any hints as to what might be wrong??

import arcpy, traceback
from arcpy import env

mxd = arcpy.mapping.MapDocument("CURRENT")

# Join Fields
fc = 'K:\TASS\4_MAPPING_DATA_SUPPORT\Traffic_Mapping\Traffic_Count_Data\2011_Counts\2011_Annual_Stations\Annual_Stations_2011.shp'
env.workspace = 'K:\TASS\4_MAPPING_DATA_SUPPORT\Traffic_Mapping\Traffic_Count_Data\District_Labels_Folder\Abilene_Labels.gdb'

fields_to_join = ['!F2011_TRAF!', '!FLAG!']
arcpy.AddField_management(fc, 'COUNT', 'TEXT')
arcpy.CalculateField_management(fc, 'COUNT', ''.join(fields_to_join), 'PYTHON')

fields_to_join = ['!TextString!', '!TFLAG!']
arcpy.AddField_management(inFeatures, 'TRAFFIC', 'TEXT')
arcpy.CalculateField-management(inFeatures, 'TRAFFIC', ''.join(fields_to_join), 'PYTHON')

lstLayers = arcpy.mapping.ListLayers(mxd)

Tags (2)
0 Kudos
11 Replies
MichelleCouden1
Deactivated User
Some more information. It says it doesn't like my shapefile which is defined at fc. Is there something I need to do to the shapefile for it to add a field??
0 Kudos
ChrisSnyder
Honored Contributor
Do you mean join (as in a tabular join) or string concatenattion (as in join the text strings 'A' and 'B' so that the new value is 'AB')?

I think you mean the latter, so try something like:

arcpy.CalculateField_management(inFeatures, 'NEWFIELD', str(!FIELD1!) + str(!FIELD2!), 'PYTHON')


The Python .join method is for Python-specific strings, which is very different than the ArcGIS tabular concatenation I think you wanting to implement. For example:

list = ["A","B","C"]
print "-".join(list)
>>> A-B-C


BTW: You second to last line in your code: arcpy.CalculateField-management needs to be arcpy.CalculateField_management (underscore not a dash). Also, I see you have cleaned up your path names - that's looks way better! Although if I were you, I would get rid of the '4_MAPPING_DATA_SUPPORT' name (don't start a folder name with a number)! If you are interested, here's some aditional unsoliced 🙂 advice pertaining to GIS naming conventions: http://forums.arcgis.com/threads/21526-Trouble-with-Feature-Datasets-in-directories-with-spaces?p=13...
0 Kudos
ChrisSnyder
Honored Contributor
Sorry - per your actual question: No, there's nothing wrong with the way you are adding the "COUNT" field that I can see.... Unless you already have a field called "COUNT"... Shapefiles do have a limit on the field name lenth (10 character max), but you are not exceeding that... Also, there are a handfull of "reserved" field names you cannot use since they mess with the underlying RDBMS stuff. Forget what those are off the top of my head, but I verified that "COUNT" was not one of them (at least for shapefiles).
0 Kudos
MichelleCouden1
Deactivated User
Yes, I am trying to make 2 fields into 1 so I can compare different values between two layers. For Example, Flag will combine with 2011_Traff to make 1 field with both values. I made the correction in my calculate field but now I'm getting an error of invalid syntax in line 20 which is my calculate field line.

import arcpy, traceback
from arcpy import env

mxd = arcpy.mapping.MapDocument("CURRENT")

# Join Fields
fc = 'K:\TASS\4_MAPPING_DATA_SUPPORT\Traffic_Mapping\Traffic_Count_Data\2011_Counts\2011_Annual_Stations\Annual_Stations_2011.shp'
env.workspace = 'K:\TASS\4_MAPPING_DATA_SUPPORT\Traffic_Mapping\Traffic_Count_Data\District_Labels_Folder\Abilene_Labels.gdb'

#fields_to_join = ['!F2011_TRAF!', '!FLAG!']
arcpy.AddField_management(fc, 'COUNT', 'TEXT')
arcpy.CalculateField_management(inFeatures, 'COUNT', str(!F2011_TRAF!) + str(!FLAG!), 'PYTHON')

#fields_to_join = ['!TextString!', '!TFLAG!']
arcpy.AddField_management(inFeatures, 'TRAFFIC', 'TEXT')
arcpy.CalculateField_management(inFeatures, 'TRAFFIC', str(!TextString!) + str(!TFLAG!), 'PYTHON')

lstLayers = arcpy.mapping.ListLayers(mxd)

flayer = arcpy.mapping.ListLayers(mxd, "AADT")[0]
alayer = arcpy.mapping.ListLayers(mxd, "AADTAnnoLabel")[0]

# Search Joined 
FRows = arcpy.SearchCursor(flayer)
ARows = arcpy.SearchCursor(alayer)

ffields = arcpy.ListFields(flayer, "COUNT", "String")
afields = arcpy.ListFields(alayer, "TRAFFIC", "String")

FList = []
AList = []

for row in FRows:
    Fvalue = row.getValue("COUNT")
    FList.append(str(Fvalue))

for row in ARows:
    Avalue = row.getValue("TRAFFIC")
    AList.append(str(Avalue))

matched = set(FList) & set(AList)

for x in matched:
    exp = '"TRAFFIC" = ' + "'" + x + "'"
    arcpy.SelectLayerByAttribute_management("AADTAnnoLabel", "ADD_TO_SELECTION", exp)
    arcpy.SelectLayerByAttribute_management("AADTAnnoLabel", "SWITCH_SELECTION")
                           
0 Kudos
MichaelVolz
Esteemed Contributor
Might this line of code:

arcpy.CalculateField_management(inFeatures, 'COUNT', str(!F2011_TRAF!) + str(!FLAG!), 'PYTHON')


Need to be something like:

arcpy.CalculateField_management(inFeatures, 'COUNT', 'str(!F2011_TRAF!) + str(!FLAG!)', 'PYTHON')

I put quote around the calculated expression.
0 Kudos
ChrisSnyder
Honored Contributor
Woops - yes the expression must be in a string format of course, so

arcpy.CalculateField_management(inFeatures, 'COUNT', 'str(!F2011_TRAF!) + str(!FLAG!)', 'PYTHON')


is correct.
0 Kudos
MichelleCouden1
Deactivated User
I'm sorry Chris!! I'm trying to understand. I throw up my hands. It keeps saying it doesn't like my shapefile (Error Attached). I've checked the spelling and location 4 times and I see nothing wrong.
0 Kudos
MichaelVolz
Esteemed Contributor
Which AddField statement is it failing on?

arcpy.AddField_management(inFeatures, 'TRAFFIC', 'TEXT')

Where is the variable inFeatures defined in your script?
0 Kudos
MichelleCouden1
Deactivated User
I saw it. It was stopping at the veri first AddField. I went back to the help desk printout. Sorry, inFeatures was supposed to be defined because it is the in_Table name. I think I also see that my fc (which is my shapefile) I need to define the in_table for that as well. So my command should change to:

fc = 'K:\TASS\4_MAPPING_DATA_SUPPORT\Traffic_Mapping\Traffic_Count_Data\2011_Counts\2011_Annual_Stations\Annual_Stations_2011.shp'
env.workspace = 'K:\TASS\4_MAPPING_DATA_SUPPORT\Traffic_Mapping\Traffic_Count_Data\District_Labels_Folder\Abilene_Labels.gdb'
inFeatures = "AADTAnnoLabel"
Table = "AADT"

fields_to_join = ['!F2011_TRAF!', '!FLAG!']
arcpy.AddField_management(Table, 'COUNT', 'TEXT')
arcpy.CalculateField_management(inFeatures, 'COUNT', 'str(!F2011_TRAF!) + str(!FLAG!)', 'PYTHON')

fields_to_join = ['!TextString!', '!TFLAG!']
arcpy.AddField_management(inFeatures, 'TRAFFIC', 'TEXT')
arcpy.CalculateField-management(inFeatures, 'TRAFFIC', 'str(!TextString!) + str(!TFLAG!)', 'PYTHON')

0 Kudos