You can use shlex.split , convenient for parsing quoted strings:
>>> import shlex >>> text = 'This is "a simple" test' >>> shlex.split(text, posix=False) ['This', 'is', '"a simple"', 'test']
Doing this in non-posix mode prevents the removal of internal quotes from the split result. posix is set to True by default:
>>> shlex.split(text) ['This', 'is', 'a simple', 'test']
If you have several lines of this type of text or you are reading from a stream, you can effectively split (excluding quotes in the output) using csv.reader :
import io import csv s = io.StringIO(text.decode('utf8'))
Unless in Python 3 you need to decode a string in unicode, since all the strings are already unicode.
source share