split parameter line with quotes
Russ.Paielli at gmail.com
Fri Jan 11 21:53:00 CET 2008
On Jan 11, 10:50 am, teddyber <teddy... at gmail.com> wrote:
> first i'm a newbie to python (but i searched the Internet i swear).
> i'm looking for some way to split up a string into a list of pairs
> 'key=value'. This code should be able to handle this particular
> example string :
> i know i can do that with some regexp (i'm currently trying to learn
> that) but if there's some other way...
The problem is that you are using commas for delimiters at two
I would start by replacing the commas between quotation marks with
some other delimiter, such as spaces of semicolons. To do that, step
through each character and keep a count of quotation marks. While the
count is odd, replace each comma with the selected alternative
delimiter. While the count is even, leave the comma. [An alternative
would be to replace the commas outside the quotation marks.]
Once that is done, the problem is straightforward. Split the string on
commas (using string.split(",")). Then split each item in the list by
"=". Use the  element for the key, and use the  element for the
value (first stripping off the quotation marks if necessary). If you
need to further split each of the values, just split on whatever
delimiter you chose to replace the commas.
More information about the Python-list