convert python list of list into dictionary -
i have list of lists in python need convert json dictionary. list follows:
[ ['[{"name":"david","level":"k","xaxis":[[]],"yaxis":[[]],', '"count":[[2011, 6], [2012, 0], [2013, 11]]},'], ['{"name":"marshall","level":"n","xaxis":[[]],"yaxis":[[]],', '"count":[[2008, 130], [2009, 53], [2010, 10], [2011, 17], [2012, 0], [2013, 142]]}]'] ]
what need is:
{ [ { "name": "david", "level": "k", "xaxis": [[]], "yaxis": [[]], "count":[[2011, 6], [2012, 0], [2013, 11]] }, { "name": "diana", "level": "k", "xaxis": [[]], "yaxis": [[]], "count":[[2011, 6], [2012, 0], [2013, 11]] } ] }
so basically, dictionary containing sub lists, , each sub list having single dictionary. how can achieved have?
updating:
so, let me break down steps purpose of clarity. need data structure (that dumping .json file):
[{ "name": "david", "level": "s", "xaxis": [ [] ], "yaxis": [ [] ], "count": [ [2008, 128], [2009, 51], [2010, 10], [2011, 17], [2012, 0], [2013, 142] ] }, { "name": "diana", "level": "v", "xaxis": [ [] ], "yaxis": [ [] ], "count": [ [2008, 11], [2009, 0], [2010, 1], [2011, 2], [2012, 0], [2013, 6] ] }]
so way constructing follows:
first have 2 lists-> names , calculations:
names = ['david', 'diana'] calculations = [[[2011, 6], [2012, 0], [2013, 11]], [[2008, 130], [2009, 53], [2010, 10], [2011, 17], [2012, 0], [2013, 142]]]
where 'calculations' list of lists of lists.
from these 2 lists, creating mentioned structure follows:
import random compiler.ast import flatten names_final = [] calculations_final = [] completed = [] in names: names_final.append('{"name":"'.encode('utf-8')+a.encode('utf-8')+'","level":"'.encode('utf-8')+random.choice('abcdefghijklmnopqrstuvwxyz').encode('utf-8')+'","xaxis":[[]],"yaxis":[[]],'.encode('utf-8')) b in calculations: calculations_final.append('"count":'.encode('utf-8')+str(b).encode('utf-8')+'},'.encode('utf-8')) i,j in map(none, names_final, calculations_final): completed.append(flatten([i,j]))
then completed list, doing following modifications need:
completed[0][0]='['+completed[0][0] completed[-1][-1]= completed[-1][-1][:-1] completed[-1][-1]=completed[-1][-1]+']'
this results in output list mentioned @ top part of question. need convert json object can viewed on browser json object, based on query defined user. hope clarify. guys suggest?
if want turn list of lists of json-encoded things list of lists of whatever objects json-encoded things decode to, that's easy:
decoded = [[json.loads(value) value in inner_list] inner_list in outer_list]
and if want turn 1 big json-encoded thing, that's easy too:
encoded = json.dumps(decoded)
if want turn outer list dict (which encode json object)… well, doesn't make sense. can't have dict (or object) containing lists, have have dict mapping keys lists.
on top of that, input data isn't list of lists of valid json-encoded things; it's list of lists of un-decodable json fragments. example, fragment:
"count":[[2011, 6], [2012, 0], [2013, 11]]}
… doesn't map anything.
maybe want this:
rejoined = ''.join(''.join(inner_list) inner_list in outer_list) decoded = json.loads(rejoined)
that give this:
[{'count': [[2011, 6], [2012, 0], [2013, 11]], 'level': 'k', 'name': 'david', 'xaxis': [[]], 'yaxis': [[]]}, {'count': [[2008, 130], [2009, 53], [2010, 10], [2011, 17], [2012, 0], [2013, 142]], 'level': 'n', 'name': 'marshall', 'xaxis': [[]], 'yaxis': [[]]}]
you can re-json-encode that, or use rejoined
as-is (because it's going equivalent).
now you've explained bit more… you're making things whole lot harder on need be. there number of problems code:
- you're trying build json-encoded strings string manipulation. hard do, , unnecessary. construct object want, , call
json.dumps
on it. whenever possible, manipulate objects, not string representations. - you're
encode
-ing dozens of strings along way (and missing few of them!) instead of building big result string , callingencode
once. plus, strings you're callingencode
onstr
byte strings, notunicode
strings, doesn't make sense. finally,json.dumps
prefersunicode
strings (and encoding you), don't need @ all. in general, encoding , decoding close edge possible, rather throughout code. - you're trying create 2 complex parallel data structures , zip them together, when have zipped inputs together. if can't figure out how patch things @ end, using wrong data structure in middle.
- you're using deprecated , undocumented features
compiler.ast.flatten
,map
none
function when there ways same things—although they're not necessary here anyway. don't write python 1.5 code unless need work python 1.5.
here's simpler way write code:
completed = [] name, calculation in zip(names, calculations): completed.append({'name': name, 'level': random.choice('abcdefghijklmnopqrstuvwxyz'), 'xaxis': [[]], 'yaxis': [[]], 'count': calculation}) result = json.dumps(completed)
now completed
data structure, , result
json encoding of data structure. , problem asking doesn't arise. (and may not need result
; many web service/rpc/etc. libraries let pass objects around , automatically json-ify them you.)
Comments
Post a Comment