Dynamically create a dictionary in python 2.4 -
i have query give me results 1 one:
k1=v1 k2=v2 k3=v3 … the variable hold result tmp. wanted create dictionary results once result retrieved. tried:
result={} result.update(tmp) i got
valueerror: dictionary update sequence element #0 has length 1; 2 required.
i tried this
result.update(tmp.replace("=",":")) i got same err. don'twant substring keys , values use d(k1)=v1 build dict. there other way it? can change query , let generate results k1:v1, k2:v2,... how can dynamically create dictionary in python 2.4? thank you!
if tmp string of k1 = v1 pairs, 1 per line, you'll need parse out pairs:
result = {} result in query: key, value = result.split('=', 1) result[key.strip()] = value.strip() this presumes query returns individual 'key1=value1' strings split out key , value pairs.
only use result.update() sequence of key-value pairs, or dictionary. passing in string won't work; python treats string sequence of one-character strings, , one-character string not fit requirement key-value pair.
Comments
Post a Comment