Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Shlex splitting with brackets

I have a need to split a series of strings into 3 component parts denoted by spaces. These strings sometimes contain sublists, but always as the last component of the string.

I was previously using Shlex to achieve this with great success, but I'm not getting the desired results anymore as my most recent sub lists contain spaces of their own and that seems to throw Shlex off.

Is there an alternative to Shlex that might perform the task better?

Some examples are:

'BREAKFAST IN ["Rolled Oats","Cornflakes","French Toast"]'

and

COPIES_FOR_EXTERNAL > "0"

Which should become lists like:

['BREAKFAST','IN', '["Rolled Oats","Cornflakes","French Toast"]']

and

['COPIES_FOR_EXTERNAL','>','"0"']
like image 901
danspants Avatar asked Jun 29 '26 11:06

danspants


1 Answers

Since you know the number of components and that the sublist is always a last element you can use str.split with maxsplit parameter:

s1 = 'BREAKFAST IN ["Rolled Oats","Cornflakes","French Toast"]'
s2 = 'COPIES_FOR_EXTERNAL > "0"'

print s1.split(None, 2) # ['BREAKFAST', 'IN', '["Rolled Oats","Cornflakes","French Toast"]']
print s2.split(None, 2) # ['COPIES_FOR_EXTERNAL', '>', '"0"']
like image 185
niemmi Avatar answered Jul 01 '26 01:07

niemmi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!