Search code examples
pythonkodi

Python - How to delete rest of string after a specific word/character


I'm a complete python noob, so please go easy.

I'm currently hacking/editing a kodi plugin called pseudo library, so that it cleans up the titles of the streams I'm grabbing so that I can put it into a better looking EPG.

Currently they look like this:

[COLOR white]3E (Now - 07 - 30 That '70s Show) - .strm

I've identified the code that produces this here:

FleName = (title + ' - ' + eptitle + '.strm').replace(":"," - ")
FleName = re.sub('[\/:*?<>|!@#$/:]', '', FleName)

and edited as follows (messy I know and I'm sure there is a much better way, as I said above I'm a noob!)

FleName = (title + '.strm').replace(":"," - ").replace("[COLOR white]","").replace("[COLOR blue]","")
FleName = re.sub('[\/:*?<>|!@#$/:]', '', FleName)

This then changes the above title to:

3E (Now - 07 - 30 That '70s Show).strm

What I really want the output to be is:

3E.strm

The closest answer I can find to my problem is here:

https://stackoverflow.com/a/14599280

However I also have parentheses within parentheses to remove and the above does not solve that e.g.

Zee Cinema (Now - 19 - 15 Baazigar (1993)).strm

I've looked at strip to remove all characters after and including "(Now" but can't quite work it out. Please can someone provide a universal solution to my problem above so that whether the title is

[COLOR white]3E (Now - 07 - 30 That '70s Show) - .strm OR

[COLOR white]Zee Cinema (Now - 19 - 15 Baazigar (1993)).strm

that it outputs just the title and .strm. So in the examples above:

3E.strm
Zee Cinema.strm

Many thanks for looking and for hopefully helping me resolve my issue.


Solution

  • FileName.split(']')[1].split('(')[0].strip() + ".strm"