I'm back to trying to remove "T" from layer names in the Drawing Order.
I have 3 layers:
T123T
12T3
T123
This will remove all the T's:
layers = act_map.listLayers()
for lyr in layers:
if "T" in lyr.name:
lyr.name = lyr.name.replace("T", "")
How do I specify the first position in the string? so I'm left with:
123T
12T3
123
Solved! Go to Solution.
Try this.
layer.name is a string. So you can ask for the "T" in the first letter with ==. If this is true you change the name. But you will do this only ones. For the first letter... This is why you add the 1 in ("T","",1).
layers = act_map.listLayers()
for lyr in layers:
if "T" == lyr.name[0]:
lyr.name = lyr.name.replace("T", "", 1)
You can also use the string.lstrip (left strip) method. This will remove the given character from the start of a string, leaving the string untouched if it doesn't start with that character.
layers = [l.lstrip("T") for l in act_map.listLayers()]
Try this.
layer.name is a string. So you can ask for the "T" in the first letter with ==. If this is true you change the name. But you will do this only ones. For the first letter... This is why you add the 1 in ("T","",1).
layers = act_map.listLayers()
for lyr in layers:
if "T" == lyr.name[0]:
lyr.name = lyr.name.replace("T", "", 1)
Awesome it worked, thanks!
You can directly index into a String, but that doesn‘t seem to be what you want to do anyway, so your question is confusing. Iterators are just one way of splitting at whitespace. There are many short command when working with iterators, such as ”for element in iter“, collect, map, and so on https://mobdro.bio/ .
You can also use the string.lstrip (left strip) method. This will remove the given character from the start of a string, leaving the string untouched if it doesn't start with that character.
layers = [l.lstrip("T") for l in act_map.listLayers()]