Home > Software engineering >  Creating specific Json with python dictionaries
Creating specific Json with python dictionaries

Time:08-21

first i'm still a python newbie, so be patient with me. I'm having trouble creating this specific structure in python 3.8 on windows 8 based on random generated words:
The random generated sentences are:
sent="Lorem Ipsum. has been long time. It has. popularised."
I have to generate this json structure:

   {'datas': [{'sentence 1': 'Lorem Ipsum', 'elements 1': [{'word1': 'Lorem','word2':'Ipsum'}], 'sentence 2': 'has been long time', 'elements 2': [{'word 1': 'has','word 2':'been','word 3':'long','word 4': 'time'}], 'phrase 3': ' It has', 'elements 3': [{'word 1': 'It', 'word 2': 'has'}], 'phrase 4': 'popularised', 'elements 4': [{'word 1': 'popularised'}]}]}

I've created this code:

import json

sent="Lorem Ipsum.has been long time. It has. popularised."

def jsoncreator(strr):
    myJSON = {}
    myJSON["datas"]=[]
    elements={}
    tmpel={}
    tmp=strr.split(".")
    for el,idx in zip(tmp,range(1,len(tmp))):
        elements['sentence ' str(idx)]=el
        elements['elements' str(idx)]    =[]
        tmpliste=el.split()
        for el1,idx2 in zip(tmpliste,range(1,len(tmpliste))):
            tmpel['word' str(idx2)]=el1
            elements['elements' str(idx)].append(tmpel)
    myJSON['datas'].append(elements)
    print(myJSON)

jsoncreator(sent)

which gives me that result for the moment:

{'datas': [{'phrase 1': 'Lorem Ipsum', 'elements1': [{'word1': 'It', 'word2':
'been', '3': 'long'}], 'phrase 2': 'has been long time', 'elements2': [{'le
mma1': 'It', 'lemma2': 'been', 'lemma3': 'long'}, {'lemma1': 'It', 'lemma2': 'be
en', 'lemma3': 'long'}, {'lemma1': 'It', 'lemma2': 'been', 'lemma3': 'long'}], '
phrase 3': ' It has', 'elements3': [{'lemma1': 'It', 'lemma2': 'been', 'lemma3':
 'long'}], 'phrase 4': ' popularised', 'elements4': []}]}

Can someone please help me find the error please, i'm banging my head agains the wall and don't understand this!

CodePudding user response:

I'd do it a with help of enumerate() (it simplifies the code):

sent = "Lorem Ipsum.has been long time. It has. popularised."

i, data = 1, {}
for sentence in map(str.strip, sent.split(".")):
    if sentence == "":
        continue
    data[f"sentence {i}"] = sentence
    data[f"elements {i}"] = [
        {f"word{j}": word for j, word in enumerate(sentence.split(), 1)}
    ]
    i  = 1

print({"datas": [data]})

Prints:

{
    "datas": [
        {
            "sentence 1": "Lorem Ipsum",
            "elements 1": [{"word1": "Lorem", "word2": "Ipsum"}],
            "sentence 2": "has been long time",
            "elements 2": [
                {
                    "word1": "has",
                    "word2": "been",
                    "word3": "long",
                    "word4": "time",
                }
            ],
            "sentence 3": "It has",
            "elements 3": [{"word1": "It", "word2": "has"}],
            "sentence 4": "popularised",
            "elements 4": [{"word1": "popularised"}],
        }
    ]
}
  • Related