I have a string of the format
s = "a, b, [c, d, ....]"
how can I split it into an array with
[c, d,, ..]
into one element and a and b as another elements of the resultant array cleanly. If I try to use
s.split(',')
even [c,d, …] gets split.
If you’re on 2.6+, then
ast.literal_evalis the way to go… (assuming you have literals that is – otherwise, maybe useevalwith caution) – or, look at thepyparsinglibrary which has a “safe” eval example at http://pyparsing.wikispaces.com/Examples (look for parsePythonValue.py)literal_eval solution: