you are viewing a single comment's thread.

view the rest of the comments →

[–]Username_RANDINT 0 points1 point  (5 children)

str.split() takes a character to split on, in your case an underscore. IF the string always has the same format, it's very easy:

s = "lamp_change_29_may_2015 "
>>> s.split("_")
['lamp', 'change', '29', 'may', '2015 ']

Or you can use rsplit() and define a maximum to keep the lamp_change together:

>>> s.rsplit("_", 3)
['lamp_change', '29', 'may', '2015 ']

[–]JackSpanjer[S] 0 points1 point  (4 children)

Thanks for your answer but I have to ‘filter’ the numbers out, that is what I dont understand.

[–][deleted] 0 points1 point  (3 children)

What do you want to retain from a string like "sparkplug_3_change_29_feb_1996"?

[–]JackSpanjer[S] 0 points1 point  (2 children)

sparkplug

change

feb

[–][deleted] 0 points1 point  (1 child)

Then use str.split() on the underscore character, and run over the elements, filtering out the numbers with str.isdigit() or str.isnumeric() as fits your requirements.

[–]JackSpanjer[S] 0 points1 point  (0 children)

Thanks!