Tokenise line containing string literals
<p>Using <code>str.split</code> on <code>"print 'Hello, world!' times 3"</code> returns the list <code>["print", "'Hello,", "world!'", "times", "3"]</code>. However, I want the result <code>["print", "'Hello, world!'", "times", "3"]</code>. How can I do that?</p>

<p>If you're going to exclude the words in quote out of the <em>split</em>, you could use <a href="https://docs.python.org/2/library/shlex.html#shlex.split" rel="nofollow"><code>shlex.split</code></a>:</p>

<pre><code>import shlex

s = "print 'Hello, world!' times 3"
print(shlex.split(s))
# ['print', 'Hello, world!', 'times', '3']
</code></pre>

python string parsing