split string on commas but ignore commas with in single quotes and create a dictionary after string split in python
<p>I  have a string as shown below,</p>

<pre><code>someVariable1='9',someVariable2='some , value, comma,present',somevariable5='N/A',someVariable6='some text,comma,= present,'
</code></pre>

<p>I have to split above string on commas but ignore commas within quotes in python and i have to create a dictionary to get the values of variables.</p>

<p>Example:</p>

<pre><code>somedictionary.get('someVariable1')
</code></pre>

<p>I am new to python please help me how can i achieve this in python</p>

<p>Build a copy of the string, looping through each character of the original string, and keeping track of the number of single-quotes you've encountered.</p>

<p>Whenever you see a comma, refer to the single-quote count.  If it's odd (meaning you're currently inside a quoted string), don't add the comma onto the string copy; instead add some unique placeholder value (i.e. something like  PEANUTBUTTER that would never actually appear in the string.)</p>

<p>When you're finished building the string copy, it won't have any commas inside quotes, because you replaced all those with PEANUTBUTTER, so you can safely split on commas.</p>

<p>Then, in the list you got from splitting, go back and replace PEANUTBUTTER with commas.</p>

python string python-3.x python-2.x