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"']
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"']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With