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'"