0

I have a string as shown below,

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

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.

Example:

somedictionary.get('someVariable1')

I am new to python please help me how can i achieve this in python

Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
Sreekanth R
  • 125
  • 1
  • 13

2 Answers2

3

Try this regular expression ,(?=(?:[^']*\'[^']*\')*[^']*$) for splitting:

import re
re.split(",(?=(?:[^']*\'[^']*\')*[^']*$)",s)

# ["someVariable1='9'",
#  "someVariable2='some , value, comma,present'",
#  "somevariable5='N/A'",
#  "someVariable6='some text,comma,= present,'"]
  • This uses look ahead syntax (?=...) to find out specific comma to split;
  • The look up pattern is (?:[^']*\'[^']*\')*[^']*$
  • $ matches the end of string and optionally matches non ' characters [^']*
  • Use non-captured group (?:..) to define a double quote pattern [^']*\'[^']*\' which could appear behind the comma that can acts as a delimiter.

This assumes the quotes are always paired.

To convert the above to a dictionary, you can split each sub expression by =:

lst = re.split(",(?=(?:[^']*\'[^']*\')*[^']*$)",s)
dict_ = {k: v for exp in lst for k, v in [re.split("=(?=\')", exp)]}

dict_

# {'someVariable1': "'9'",
#  'someVariable2': "'some , value, comma,present'",
#  'someVariable6': "'some text,comma,= present,'",
#  'somevariable5': "'N/A'"}

dict_.get('someVariable2')
# "'some , value, comma,present'"
Psidom
  • 209,562
  • 33
  • 339
  • 356
  • Thanks a lot @Psidom,@John for the detailed explanation, It is working perfectly. – Sreekanth R Aug 30 '16 at 03:26
  • when i execute the same piece of code in python 2.4.6 i am getting below error , detailsdict = {k: v for exp in detailslist for k, v in [re.split("=(?=\')", exp)]} ^ SyntaxError: invalid syntax – Sreekanth R Aug 30 '16 at 03:52
  • 1
    @sreekanthramagiri dictionary comprehensions were added in `Python 2.7` if I'm not mistaken. *Don't use `Python 2.4.6`*. – Dimitris Fasarakis Hilliard Aug 30 '16 at 04:06
  • Check this. http://stackoverflow.com/questions/1747817/create-a-dictionary-with-list-comprehension-in-python dict((k, v) for ...) should work for the old version. – Psidom Aug 30 '16 at 04:14
  • How did you come up with this expression? I don't think I will ever be able to come up with this expression. – Nguai al May 20 '20 at 19:11
0

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.

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.)

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.

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

John Gordon
  • 29,573
  • 7
  • 33
  • 58