How can I trim a field name

05-14-2019 08:24 AM
by Anonymous User
Not applicable


I have a script that copies fields from on feature class to another. But it stops running if it encounters a field name greater than 10 characters in the source file. I am wondering if it's possible to trim the field name to let the program keep running. Something like this: 

... shp = 'WillCounty_Street_09282018.shp'
... index = 8
... fields = arcpy.ListFields(shp)
... for field in fields:
...     print ("name is",, "length is", len(
...     if len( > index:
... =[0: index+1:]

I made the index = 8 because the the feature class I am testing has no field names greater than 10. Of course that code works only works with strings. It runs but does not do the job of trimming the field names. Is it possible to do this? 

Tags (1)
0 Kudos
1 Reply
MVP Legendary Contributor

It is a shapefile problem, gdb featureclass tables don't have this problem

but you could include ValidateFieldName in your code

Validating table and field names in Python—ArcPy Get Started | ArcGIS Desktop