How to extract keys from dictionaries and make lists

Asked 1 weeks ago, Updated 1 weeks ago, 1 views

I'm going to read the json file in dictionary form and make a list of the key values I want and save this list as csv.

In the process of reading it into a dictionary, changing it to a list, and converting it to csv, I ran a code that saved the desired key value in a column, but the result was not printed as I thought.

json file is

{
    "id": "EXSA2002108040",
    "metadata": {
        "title": "National Institute of Korean Language Web corpus extraction EXSA2002108040",
        "creator": "National Language Institute",
        "distributor": "National Institute of Languages",
        "year": "2020",
        "category": ["Web>Review>Nuri Network", "Web>Review>Blog"],
        "annotation_level": ["Sentimental Analysis"],
        "sampling": "Partial extraction - Partial extraction"
    },
    "document": [{
        "document_id": "ERRW1903002753.6",
        "document_score": 1,
        "metadata": {
            "title": "Half bath herbal tea bag",
            "author": "June",
            "publisher": "egloos",
            "date": "20090524",
            "url": "http://kimmari02.egloos.com/2389745"
        },
        "paragraph": [{
                "paragraph_id": "ERRW1903002753.6.1",
                "Paragram_form": "As I entered the xx line, I was like that for a long time, but I'm more focused on my health." Friends around them also start walking along the path one by one, and are told by her, who is seemingly too healthy, that she is using a tea bag of herbal medicine for bathing. I also bought it according to a message that it is good because if my menstrual pain is severe, I want to go home after everything is gone. 24 tea bags of weed and wormwood... "LOL"
            },
            {
                "paragraph_id": "ERRW1903002753.6.2",
                "Paragram_form": "It's neatly packed with nonwoven fabric, so just put it in hot water and stir it a little bit, and it'll come out quickly." I put it in every bath (I take a shower often, but I don't take a bath that often.. This is also a medicinal herb, so the use is on its own;;) The smell of fresh medicinal herbs spread in the bathroom, making me feel better. It's not the right time yet, so I don't know about the effect, but I hope it works on itchy skin. They also sell mints, but I'm satisfied now, so I want to buy mints fresh next time. I was wondering if they sell old ones, but the mugwort scent itself is fresh like peppermint, clean packaging, and easy to use. I think the internet market in Korea is good, too. When I ordered it, if I just put it in water, it would make it really hard and deliver it to my house."
            },
            {
                "paragraph_id": "ERRW1903002753.6.3",
                "Paragram_form": "I didn't like putting bath bombs in my bath before, but when I changed it to herbal medicine, I don't know about medicine, but I feel better, and I don't think it's a waste of money." You have to use this continuously, so if you feel better in two or three months, upload it. -_-"
            }
        ]
    }]
}

It's in this format.

There are two keys I want: 'paragram_id' and 'paragram_form', how can I make a list of those two keys?

python

2022-09-20 15:02

1 Answers

a = { "document": [ {"id": "1", "paragraph": [ {"id":"num1", "form":"content1"}, {"id":"num2", "form": "content2"} ] } ] }

for i in a['document']:
    for j in i['paragraph']:
        print(j['id'], j['form'])

# # result
#('num1', 'content1')
#('num2', 'content2')


2022-09-20 15:02

If you have any answers or tips


© 2022 pinfo. All rights reserved.