Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split a filename python on underscore

I have a filename as "Planning_Group_20180108.ind". i only want Planning_Group out of it. File name can also be like Soldto_20180108, that case the output should be Soldto only.

A solution without using reg ex is more preferable as it is easier to read for a person who haven't used regex yet

like image 674
user7422128 Avatar asked Oct 20 '25 21:10

user7422128


1 Answers

The following should work for you

s="Planning_Group_20180108.ind"
'_'.join(s.split('_')[:-1])

This way you create a list which is the string split at the _. With the [:-1] you remove the last part. '_'.join() combines your list elements in the resulting list.

like image 181
jan-seins Avatar answered Oct 22 '25 11:10

jan-seins