Select to view content in your preferred language

How to remove a character from Drawing order layer name using arcpy?

1521
7
Jump to solution
08-01-2023 09:33 AM
Labels (3)
Davec43
Regular Contributor

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?

1 Solution

Accepted Solutions
RichlandGIS
Frequent Contributor

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_

View solution in original post

7 Replies
RichlandGIS
Frequent Contributor

That is probably because you are not allowed to have geodatabase feature class name starting with a number.

R_

0 Kudos
AngelaSchirck
Frequent Contributor

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

0 Kudos
Davec43
Regular Contributor

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.

 

DELETE.png

 

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

0 Kudos
RichlandGIS
Frequent Contributor

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.

R_

Davec43
Regular Contributor

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 characters
Failed to execute (Rename).

 

I'm assuming there isn't going to be a way around this?

0 Kudos
RichlandGIS
Frequent Contributor

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_

Davec43
Regular Contributor

Thats how I'll have to do it. Thanks!

0 Kudos