different names for types with arcpy.FieldList

413
3
08-23-2011 07:35 AM
AdamSlayton
New Contributor
I recently created a python script that iterates through a directory, identifies all feature classes within a directory, ids all fields within the FC, and outputs the the name of the fields and the type of the field.  The code for reporting field names and types was essentially:

fList = arcpy.FieldList(currentFC)

for f in fList:
     echo = f.name + ", " + f.type
     print echo
    
I noticed the the field types that were reported were not what I expected (i.e. SHORT, DOUBLE, DATE etc.).  The types that were output were Integer, Smallinteger etc.  My ultimate goal is to add a field to a feature class with the same name and type as what I found, but the reported types are not allowed as the required field_type parameter in the arcpy.AddField_management method.

Is there another way to get field types that produce the allowable entries for the AddField method?

Thanks,

Adam Slayton
Tags (2)
0 Kudos
3 Replies
JoelCalhoun
New Contributor III
I have also run into this issue.  At first I set up a complex set of if/else statements to handle this but then someone recommended using a simple python dictionary to convert the reported type to the desired type.

fieldTypeDictionary = {'SmallInteger': 'SHORT', 'Integer': 'LONG', 'Single': 'FLOAT', 'Double': 'DOUBLE', 'String': 'TEXT', 'Date': 'DATE'}
fldType = searchTheField.field_type
arcpy.AddField_management(updateDSParam,updateFieldParam,fieldTypeDictionary[fldType], fldPrecision, fldScale, fldLength)


I hope this helps, good luck.


Joel
0 Kudos
AdamSlayton
New Contributor
I have also run into this issue.  At first I set up a complex set of if/else statements to handle this but then someone recommended using a simple python dictionary to convert the reported type to the desired type.

fieldTypeDictionary = {'SmallInteger': 'SHORT', 'Integer': 'LONG', 'Single': 'FLOAT', 'Double': 'DOUBLE', 'String': 'TEXT', 'Date': 'DATE'}
fldType = searchTheField.field_type
arcpy.AddField_management(updateDSParam,updateFieldParam,fieldTypeDictionary[fldType], fldPrecision, fldScale, fldLength)


I hope this helps, good luck.


Joel





I did that at first then realized unless it was the SmallInteger to SHORT conversion, all I needed was the str.uppper() method.  Still, this seems like a bug.  Anyone know how to report such for arcpy?

Adam Slayton
0 Kudos
JoelCalhoun
New Contributor III
Adam,

There are at least 4 that need a conversion other than the upper case issue.

'SmallInteger': 'SHORT'
'Integer': 'LONG'
'Single': 'FLOAT'
'String': 'TEXT'

I agree that it is strange that you cannot simply return the field type and directly use the returned value to add a new field.
0 Kudos