
Really, you could also just use re.match: import re pat = re.compile(r'(\d\d):(\d\d):(\d\d),(\d{3}) --> (\d\d):(\d\d):(\d\d),(\d{3})') def parse(string): return pat.match(string) ... print(parse('00:02:34,452 --> 00:02:37,927')) # prints ['00', '02', '34', '452', '00', '02', '37', '927'] That way, if the input is invalid, `None` will be returned, so you have free error checking (sort of). On Mon, Sep 28, 2015 at 5:23 PM, Ryan Gonzalez <rymg19@gmail.com> wrote:
import re parts = re.split(':|(-->)|,', '00:02:34...')
On Mon, Sep 28, 2015 at 5:10 PM, Niilos <niilos@gmx.com> wrote:
Hello everyone,
I was wondering how to split a string with multiple separators. For instance, if I edit some subtitle file and I want the string '00:02:34,452 --> 00:02:37,927' to become ['00', '02', '34', '452', '00', '02', '37', '927'] I have to use split too much time and I didn't find a "clean" way to do it. I imagined the split function with an iterator as parameter. The string would be split each time its substring is in the iterator.
Here is the syntax I considered for this :
'00:02:34,452 --> 00:02:37,927'.split([ ':', ' --> ', ',' ]) ['00', '02', '34', '452', '00', '02', '37', '927']
Is it a relevant idea ? What do you think about it ?
Regards, Niilos. _______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
-- Ryan [ERROR]: Your autotools build scripts are 200 lines longer than your program. Something’s wrong. http://kirbyfan64.github.io/
-- Ryan [ERROR]: Your autotools build scripts are 200 lines longer than your program. Something’s wrong. http://kirbyfan64.github.io/