n1k
(n1k)
February 23, 2015, 12:04pm
1
Is it possible to remove suffix from a string?
For example. If i do something like this:
for obj in bpy.context.selected_objects:
if obj.type == “MESH”:
print(obj.name)
i get this
Cube.002
Cube.001
Cube
How to tell python to remove suffix so I get
Cube
Cube
Cube
Cheers,
n1k
DoubleZ1
(DoubleZ_)
February 23, 2015, 12:44pm
2
What about using “split()” method ?
originalString = "Cube.002"
strArray = originalString.split(".")
print(strArray[0])
Of course, you can get directly the wanted part :
originalString = "Cube.002"
print(originalString.split(".")[0])
JohnVV
(JohnVV)
February 23, 2015, 2:42pm
3
you can as above
BUT
how do you expect python or the operating system or any program to know what one to use
you have 3 files with identical names
software is not magic it only dose what YOU instruct it to do
trying to have more than ONE file with the EXACT same name will cause major problems and even ERRORS and warnings about overwriting a file
CoDEmanX
(CoDEmanX)
February 23, 2015, 3:00pm
4
It should probably be more like:
def strip_suffix(s):
head, tail = s.rsplit(".")
if tail.isnumeric():
return head
return s
strip_suffix(obj.name)
# "foo.bar" -> "foo.bar"
# "val.10e-5" -> "val.10e-5"
# "Cube.001" -> "Cube"
# "Cube.234" -> "Cube"
n1k
(n1k)
February 25, 2015, 9:42am
5
Hi guys. Thanks for the help I learned a lot from your examples and efficiently tackled problem in my script:)
Cheers,
n1k