2

I am trying to edit the length of all the text fields in a feature class. The feature class has a lot of fields so I want to do this in python. I only have access to v10.1 so cannot use the AlterField_management option.

The way I'm going about it is to use fc to fc conversion with an appropriate field mapping.

Below is my code but it does not alter the length of the text fields as intended. The fc to fc conversion goes ahead but with no changes to any of the field properties. I'm pretty sure the problem is with the field mapping but I can't figure it out.

import arcpy

infc =  r"C:\path to original fc"

out_space = r"C:\path to out gdb"
out_fc = "fc_newfieldlengths"

# set up field mappings object
fms = arcpy.FieldMappings()


# Create list of all non-string fields to skip when creating the fieldmaps

text_fields = arcpy.ListFields(infc, field_type = 'String')
all_fields = arcpy.ListFields(infc)

skip_fields = [x for x in all_fields if x not in text_fields]


# create field mapping with new length for the text fields
for field in all_fields:
    if field in skip_fields:
        pass
    else:   
        fm = arcpy.FieldMap()

        fm.addInputField(infc, field.name)

        outfield = fm.outputField
        outfield.length = 150
        outfield.name = field.name

        fm.outputField = outfield

        fms.addFieldMap(fm)


# Copy featureclass with new field mapping
arcpy.FeatureClassToFeatureClass_conversion(infc, out_space, out_fc, field_mapping = fms)

# End of script

If I tried only looping through a list of string fields to generate the fieldmaps I found that all the fields were changed to string fields in the output, not just the fields I am interested in amending.

To avoid changing the data type for all the fields I created a skip list of fields not to alter - this manages to preserve the datatypes but has not amended the text field lengths (as was occurring previously but just also annoyingly changing all the datatypes as well).

I suspect I just don't understand field mappings well enough to know what I'm doing wrong.

1 Answer 1

2

Your code probably fails because you're trying to compare field objects. Instead, you should compare field names.

skip_fields = [x.name for x in all_fields if x not in text_fields]
for field in all_fields:
    if field.name in skip_fields:
        pass

Here's a code variation that works for me:

inFc = r"C:\Workspace\Workspace.gdb\FM_in"
outPath = r"C:\Workspace\Workspace.gdb"
outName = "FM_out"

import arcpy

fms = arcpy.FieldMappings ()

shapeFld = arcpy.Describe (inFc).shapeFieldName
oidFld = arcpy.Describe (inFc).OIDFieldName

for field in arcpy.ListFields (inFc):
    if field.name in (shapeFld, oidFld):
        continue

    if field.type == "String":

        fm = arcpy.FieldMap ()
        fm.addInputField (inFc, field.name)
        field.length = 150
        fm.outputField = field
        fms.addFieldMap(fm)

    else:
        fm = arcpy.FieldMap ()
        fm.addInputField (inFc, field.name)
        fms.addFieldMap(fm)

arcpy.FeatureClassToFeatureClass_conversion (inFc, outPath, outName, "", fms)
2
  • Thanks Emil, your code worked no problem. Can you explain why you use the shapeFieldName and OIDFieldName?
    – ASeaton
    Commented Dec 3, 2015 at 9:49
  • I found that adding those two fields to the fieldsmapping object caused a crash with feature class to feature class. So I exclude them. Those fields are automatically generated when a new feature class is created. Commented Dec 3, 2015 at 15:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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