Select to view content in your preferred language

Change field length using arcpy.FeatureClassToFeatureClass_conversion

7917
9
Jump to solution
05-14-2015 11:20 AM
MichaelCarson4
Emerging Contributor

My goal is to take a shapefile, change the length of one of the text fields, and save a new shapefile using arcpy.FeatureClassToFeatureClass_conversion.  You can do it using the tool's interface, just add a shapefile to it and the fields are listed, right click on a field to change it's properties and change the field length, then save to a new shapefile.

But how to do that using code?  I think you have to manipulate the field mapping to do it, but I cannot seem to find where you make the change for a particular field.  So far I have:

>>> inshp = "c:/temp/test1.shp"

>>> fms = arcpy.FieldMappings()

>>> fms.addTable(inshp)

>>> fms.fieldCount

6

>>> fm = fms.getFieldMap(0)

>>> fm.getInputFieldName(0)

u'CITY_NAME'

So how do I change the field length for the CITY_NAME field and save it back to the field mapping?  Once that is done, I guess you put the field mapping back into the tool:

arcpy.FeatureClassToFeatureClass_conversion(inshp, "c:/temp", "test2.shp", "", field_mapping=fms)

0 Kudos
1 Solution

Accepted Solutions
DarrenWiens2
MVP Honored Contributor

The following changes the length of the field.

>>> inshp = "line"
... fms = arcpy.FieldMappings()
... fm = arcpy.FieldMap()
... fm.addInputField(inshp,"myField")
... newField = fm.outputField
... newField.length = 100
... fm.outputField = newField
... fms.addFieldMap(fm)
... arcpy.FeatureClassToFeatureClass_conversion(inshp, r'C:/junk', 'newshp', field_mapping=fms)

Note that you must make your changes in a new field object ("newField") rather than fm.outputField. I won't say I understand why this is, it just is. For example, this will not change the output field length, although I would have guessed, like you, that it should:

>>> inshp = "line"
... fms = arcpy.FieldMappings()
... fm = arcpy.FieldMap()
... fm.addInputField(inshp,"myField")
... fm.outputField.length = 100
... fms.addFieldMap(fm)
... arcpy.FeatureClassToFeatureClass_conversion(inshp, r'C:/junk', 'newshp2', field_mapping=fms)

View solution in original post

9 Replies
IanMurray
Honored Contributor

Why not take the output of your manual FC to FC conversion and copy it as a python snippet and see what is going on(Go to Geoprocessing Results, navigate to the process, right click and select copy as python snippet)?  From there it should be fairly easy to determine what changes you want to make and how to manipulate fields.

0 Kudos
MichaelCarson4
Emerging Contributor

I have, but it does not tell me what code to use to make it that way! -mike

0 Kudos
DanPatterson_Retired
MVP Emeritus

hmmm .... maybe a combination of

Add Field   ...

Calculate field ...

Delete Field ...

and/or ...

Transpose Fields    ...

will get you where you want since

Alter Fields  only allows you to change the field name

0 Kudos
MichaelCarson4
Emerging Contributor

Wow, that's a lot of tools to use!  I'm getting closer in my code, but cannot change the field length so far:

>>> fm.outputField.name

u'CITY_NAME'

>>> fm.outputField.length

25

>>> fm.outputField.length = 100

>>> fm.outputField.length

25

0 Kudos
DanPatterson_Retired
MVP Emeritus

a couple of lines of code... to get you started

env.workspace = "C:/somefolder/a.gdb"

arcpy.AddField_management("some_layer", "cool_name", "DOUBLE", 12,6)   # double field 12 wide 6 decimals

arcpy.CalculateField_management("some_layer", "cool_name", "!old_field!", "PYTHON_9.3")

...

Etc

0 Kudos
DarrenWiens2
MVP Honored Contributor

The following changes the length of the field.

>>> inshp = "line"
... fms = arcpy.FieldMappings()
... fm = arcpy.FieldMap()
... fm.addInputField(inshp,"myField")
... newField = fm.outputField
... newField.length = 100
... fm.outputField = newField
... fms.addFieldMap(fm)
... arcpy.FeatureClassToFeatureClass_conversion(inshp, r'C:/junk', 'newshp', field_mapping=fms)

Note that you must make your changes in a new field object ("newField") rather than fm.outputField. I won't say I understand why this is, it just is. For example, this will not change the output field length, although I would have guessed, like you, that it should:

>>> inshp = "line"
... fms = arcpy.FieldMappings()
... fm = arcpy.FieldMap()
... fm.addInputField(inshp,"myField")
... fm.outputField.length = 100
... fms.addFieldMap(fm)
... arcpy.FeatureClassToFeatureClass_conversion(inshp, r'C:/junk', 'newshp2', field_mapping=fms)
MichaelCarson4
Emerging Contributor

Thanks Darren!  That does the trick and results in a shapefile with the one changed field.  If I want to bring over the others as is, I would loop through each to create a field map and then add them to the field mappings variable.  Then I would have a shapefile with all the fields and the one I changed too.

0 Kudos
BlakeTerhune
MVP Regular Contributor
MichaelCarson4
Emerging Contributor

Done!

0 Kudos