python - json.dump() gives me "TypeError: keys must be a string" -
i have simple code takes dictionary tuple key , turns json:
in [11]: import simplejson json in [12]: data = {('category1', 'category2'): 4} in [13]: json.dumps(data) however, running code gives me:
typeerror: keys must string i have tried str()'ing keys , else can find, without luck.
the error seems pretty clear: keys must strings.
if take example question , str()-ify keys:
>>> data = {str(('category1', 'category2')): 4} it works fine:
>>> json.dumps(data) '{"(\'category1\', \'category2\')": 4}' having said that, in position consider making keys more readable. possibly like:
>>> data = dict((':'.join(k), v) k,v in data.items()) this turns key ('category1', 'category2') category1:category2,
Comments
Post a Comment