Home > front end >  Dictionary to a json file using Python
Dictionary to a json file using Python

Time:12-16

I want to create a dictionary using the values in a csv file and values defined in the code. Then need to write that into a json file.

The image in the below link shows the data in the csv when attached to a df.

csv data

I used the following code.

import json
import pandas as pd

df = pd.read_csv('text.csv')

dict = {}

for index, row in df.iterrows():
    a = "aaa"
    u = "uuu"
    g = str(row['animal'])
    h = str(row['characteristic'])


    dict.update({
        "journal": [
            [f"{a}",f"{g}"],
            [f"t_s{u}",f"{h}"]
        ]})


    with open('web.json', 'a') as file:
        json.dump(dict, file)
        file.write('\n')

Above gave the output as below in 'web.json' file:

{"journal": [["aaa", "dog"], ["t_suuu", "four legs"]]}
{"journal": [["aaa", "cat"], ["t_suuu", "four legs"]]}
{"journal": [["aaa", "cow"], ["t_suuu", "four egs"]]}
{"journal": [["aaa", "bird"], ["t_suuu", "two legs"]]}
{"journal": [["aaa", "ant"], ["t_suuu", "six legs"]]}

CodePudding user response:

Instead of using dict to record data and saving to JSON file on every loop, use List and append the object to the list on each loop. After loop exits, dump that list of data (array of objects) to JSON file.

import json
import pandas as pd

df = pd.read_csv('text.csv')

dict = []

for index, row in df.iterrows():
    a = "aaa"
    u = "uuu"
    g = str(row['animal'])
    h = str(row['characteristic'])


    dict.append({
        "journal": [
            [f"{a}",f"{g}"],
            [f"t_s{u}",f"{h}"]
        ]})


with open('web.json', 'a') as file:
    json.dump(dict, file)

# Later you can delete `dict` (containing objects array) variable from memory
# del dict

CodePudding user response:

You can try the following code

import json
import pandas as pd

df = pd.read_csv('text.csv')

final_data = []
for index, row in df.iterrows():
    dict = {}
    for key,value in zip(row.index, row.values):
        dict[key] = value

    final_data.append(dict)


with open('web.json', 'w') as file:
    json.dump(final_data, file)

As @pratik-ghodke mentioned you should be using a List instead of dict to store the data and then dump the List to the json file.

The code above will store the data to web.json file in the following format

[{"animal": "dog", "characteristic": "four legs", "groups": "mammal"}, {"animal": "cat", "characteristic": "four legs", "groups": "mammal"}]
  • Related