[Python-ideas] list as parameter for the split function

Chris Angelico rosuav at gmail.com
Tue Sep 29 00:27:23 CEST 2015


On Tue, Sep 29, 2015 at 8:10 AM, Niilos <niilos at gmx.com> wrote:
> 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 ?

Two possibilities:

1) Replace all separators with the same one.
'00:02:34,452 --> 00:02:37,927'.replace(",",":").replace(" --> ",":").split(":")

2) Use a regular expression.
re.split(":|,| --> ",'00:02:34,452 --> 00:02:37,927')
# or working the other way: find all the digit strings
re.findall("[0-9]+",'00:02:34,452 --> 00:02:37,927')

You could also consider a more full parser; presumably splitting into
strings is just the first step. I don't have anything handy in Python,
but there would be ways of doing the whole thing in less steps.

ChrisA


More information about the Python-ideas mailing list