I looked around and found a few examples of how to split text in python but having problems on my example. Here’s what I want to parse:
<img alt="" src="http://example.com/servlet/charting?base_color=grey&chart_width=288&chart_height=160&chart_type=png&chart_style=manufund_pie&3DSet=true&chart_size=small&leg_on=left&static_xvalues=10.21,12.12,43.12,12.10,&static_labels=blue,red,green,purple">
Here’s what I tried:
dict(kvpair.split('=') for kvpair in variableIwantToParse.split('&'))
I get the error “ValueError: dictionary update sequence element #0 has length 5; 2 is required”
I tried also to use variableIwantToParse.strip(‘&’) but when I tried to print variableIwantToParse it only displaced one letter at a time.
I’m sure this is easy but can’t seem to figure out how to parse it. I basically want 10.21,12.12,43.12,12.10 to be associated with blue,red,green,purple (in the order displayed)
Thanks very much for your help(and sorry if this is too easy..I just can’t for the life of me figure out the command to parse this) 🙂
Use the built-in urlparse module, do not do these splits yourself.
If you’re using Python with a version less than 2.6, then you have to import the cgi module. Do this instead:
Then to associate them to a dictionary, use the provided dict constructor alongside zip.