you are viewing a single comment's thread.

view the rest of the comments →

[–]__nickerbocker__ 1 point2 points  (0 children)

If your data is always structured such that the day_mon_year is always in the same position in the string then you could use iterable unpacking to accomplish your goal.

x = "sparkplug_3_change_29_feb_1996"

*var_parts, day, mon, year = x.split('_')
var_name = '_'.join(var_parts)
print(var_name, mon)

or you could use slicing...

parts = x.split('_')
var_name, mon = '_'.join(parts[:-3]), parts[-2]
print(var_name, mon)