Zauberin Stardreamer
Zauberin Stardreamer

Reputation: 1344

Convert a string to a whitespace separated list w/quoted elements

Is there a simple way in Python to convert a string to a list using whitespaces as separators, but ignoring the whitespace within quoted text? IE: each word is treated as a separate search term, but any quoted text is treated as one term.

Upvotes: 3

Views: 1147

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1123410

Yes, by using the shlex.split() function:

>>> import shlex
>>> shlex.split('Some whitespace "separated string"')
['Some', 'whitespace', 'separated string']

Upvotes: 11

Related Questions