Usually when splitting strings on spaces, developers will tend to rely on str.split
, which is able to serve that purpose pretty well. But when the need to split some spaces and preserve others arises, things quickly become harder and implementing a custom solution can require investing time in proper escaping.
Just rely on shlex.split
instead of str.split
:
>>> importshlex>>>>>> text='I was sleeping at the "Windsdale Hotel"'>>> print(shlex.split(text))['I', 'was', 'sleeping', 'at', 'the', 'Windsdale Hotel']
shlex
is a module originally created to parse Unix shell code. For that reason, it supports preserving phrases through quotes. Typically, in Unix command lines, words separated by spaces are provided as arguments to the called command, but if you want to provide multiple words as a single argument, you can use quotes to group them.
That's exactly what shlex
reproduces, providing us with...