Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple splits on a single line in Python

Tags:

python

split

I would like to know if there is a more compact (or Pythonic) way of doing several splits of some input string. Now I'm doing:

[a,bc,de] = 'a,b:c,d/e'.split(',')
[b,c] = bc.split(':')
[d,e] = de.split('/')
like image 772
Josep Valls Avatar asked Jan 20 '26 19:01

Josep Valls


1 Answers

I'd use the regular expression library. You don't need to use lists for unpacking, you can use tuples as below.

import re
regex = re.compile(r'[,:/]')
a, b, c, d, e = regex.split('a,b:c,d/e')
like image 130
Ryan Avatar answered Jan 23 '26 08:01

Ryan