I have a script that creates a geodatabase and then splits a layer by its name into that geodatabase. The problem is if the layer name starts with a number "123" it'll add a "t" to the beginning of the layer name. How do I strip the "t" from the drawing order using arcpy?
Unless you want to export them to shapefiles (which do allow filenames to start with numbers) you will run into that error.
You could use something like:
string = '_' + lyr.name[1:]
which would replace the 't' with and underscore which will allow you to save them in geodatabase.
R_
I see what you're saying. I found this from stack exchange and it'll strip the first character from Layer names if they don't have a number as their second character.
fcs = arcpy.ListFeatureClasses() for fc in fcs: Arcpy.Rename_management(fc,fc[1:])
When I try it with a T123... I get
ERROR 000354: The name contains invalid charactersFailed to execute (Rename).
I'm assuming there isn't going to be a way around this?
lyr is a Layer class, need to grab the layer name from it as string. Think this is what you are looking for:
for lyr in layer: string = lyr.name[1:]
Though, you will still get an error if you try to save them with a name starting with a number in a geodatabase.
Here's a snippet of a portion of the output. Since the layer starts with a number, arc adds a "T". I'm trying to script removal of all the "T's" since it can be tedious manually removing all of them.
When I try this:
import arcpy Map_name = map1 aprx = arcpy.mp.ArcGISProject("CURRENT") act_map = aprx.activeMap map = aprx.listMaps(Map_name) layer = act_map.listLayers() for lyr in layer: string = lyr[1:]
I get:
Traceback (most recent call last):File "<string>", line 8, in <module>TypeError: 'Layer' object is not subscriptable
Not exactly sure what you're trying to do, but to get rid of a leading t in python:
string = "t123abc"
newstring = string[1:] //newstring will be everything but the first character of the original string
That is probably because you are not allowed to have geodatabase feature class name starting with a number.
Les membres connectés peuvent publier, suivre les mises à jour, et plus encore. Nouveau ici ? Inscrivez-vous gratuitement.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.