Creating a tripled nested JSON in Python -


i trying create json via first making python dict produces following structured format:

{"sentences": [{"sentence": "at end of november 2005 , hong kong , america had 132 licensed banks , 41 restricted licensed banks , 35 deposit-taking institutions , , 86 representative offices .","parsedsentence": "xyz in text e.g. @ end of november 2005 , location_slot , location_slot had number_slot licensed banks , number_slot restricted licensed banks , number_slot deposit-taking institutions , , number_slot representative offices .","location-value-pairs": [{"america": 132}, {"america": 41}, {"america": 35},            {"hong kong": 132}, {"hong kong": 41}, {"hong kong": 35}]}]} 

however can't seem create code of 2 nested keys, , third key of keys, each of keys having array.

my current code structure following (note, couldn't keys "sentence", "parsedsentence" etc created). note have no key variables (my keys strings themselves) want move out of in future can traverse python dictionary quicker:

for sentence in parsedsentences:        wordsinsentence = []        token in sentence["tokens"]:             wordsinsentence.append(token["word"])        sentence = " ".join(wordsinsentence)              locationtokenids, location in tokenids2location.items():              numbertokenids, number in tokenids2number.items():                    if sentence not in sentences2location2values:                         sentences2location2values[sentence] = {}                    if location not in sentences2location2values[sentence]:                         sentences2location2values[sentence][location] = []                    sentences2location2values[sentence][location].append(number)  open(outputfile, "wb") out:         json.dump(sentences2location2values, out) 

this gives me json looking this:

{"mobutu sese seku seized power in 1965 via coup , renaming country zaire , , reigning next 32 years head of ruthless , corrupt dictatorship .": {"zaire": [32.0]}, "\u00c3 cents \u00c2 $ \u00c2 cents movement liberation of congo -lrb- mlc -rrb- : under direction of bemba , , backed uganda , mlc formed in 1998 154 soldiers .": {"congo": [154.0], "uganda": [154.0]}, ... 

which doesn't me structure need.

how can have solution allows me fill in right keys , values 1 one @ right parts of loop, , not 1 line solution?

it seems there's of mismatch between ideal output @ beginning of question, , code does, in code doesn't create keys sentence, parsedsentence , location-value-pairs.

this may mean i've misunderstood question, if not, try like:

output = {"sentences": []}  sentence in parsedsentences:      sentencedict = {"parsedsentence": sentence}      wordsinsentence = []     token in sentence["tokens"]:          wordsinsentence.append(token["word"])     sentence = " ".join(wordsinsentence)      sentencedict["sentence"] = sentence      sentencedict["location-value-pairs"] = []      locationtokenids, location in tokenids2location.items():         numbertokenids, number in tokenids2number.items():             sentencedict["location-value-pairs"].append({location: number})      output["sentences"].append(sentencedict) 

Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -