jthedudeoflife
jthedudeoflife

Reputation: 391

Python, getting list into correct JSON format

This should be fairly straight forward however Im having trouble getting this to work. I have the following code below.

I have a csv file of which in Column A, I have the following:

[11, 12, 14, 18, 19, 23, 43, 44]

In column B, I have the following:

[2, 2, 2, 2, 2, 2, 2, 2]

My attempt is:

import numpy
import csv
import json

f = open('C:\filepath\m.csv', 'r+')
csv_f = csv.reader(f, delimiter=',', quotechar='"')
new_list = []
new_list_two = []

for row in csv_f:
    new_list.append(row[0])
    new_list_two.append(row[1])
    new_list_two= map(int, new_list_two)
    new_list= map(int, new_list)
    a = str(numpy.mean(new_list))
    b = str(numpy.mean(new_list_two))
    c = new_list + new_list_two

The follow code will return:

[11, 12, 14, 18, 19, 23, 43, 44, 2, 2, 2, 2, 2, 2, 2, 2]

I want to get my code to assign it to a JSON file by adding it to a dictionary and having the output like such:

[
    {
        "September": 11,
        "medium-critical": 2
    },
    {
        "September": 12,
        "medium-critical": 2
    },
    {
        "September": 14,
        "medium-critical": 2
    },
    {
        "September": 18,
        "medium-critical": 2
    },
    {
        "September": 19,
        "medium-critical": 2
    },
    {
        "September": 23,
        "medium-critical": 2
    },
    {
        "September": 43,
        "medium-critical": 2
    },
    {
        "September": 44,
        "medium-critical": 2
    },
    {
        "September": 20,
        "medium-critical": 3
    },
    {
        "September": 32,
        "medium-critical": 3
    },
    {
        "September": 33,
        "medium-critical": 3
    },
    {
        "September": 54,
        "medium-critical": 3
    },
    {
        "September": 57,
        "medium-critical": 3
    },
    {
        "September": 69,
        "medium-critical": 3
    },
    {
        "September": 70,
        "medium-critical": 3
    },
    {
        "September": 73,
        "medium-critical": 3
    },
    {
        "September": 58,
        "medium-critical": 6
    },
    {
        "September": 66,
        "medium-critical": 6
    },
    {
        "September": 100,
        "medium-critical": 7
    },
    {
        "September": 68,
        "medium-critical": 9
    },
    {
        "August": 23,
        "medium-critical": 2
    },
    {
        "August": 44,
        "medium-critical": 2
    },
    {
        "August": 19,
        "medium-critical": 2
    },
    {
        "August": 18,
        "medium-critical": 2
    },
    {
        "August": 43,
        "medium-critical": 2
    },
    {
        "August": 48,
        "medium-critical": 2
    },
    {
        "August": 53,
        "medium-critical": 3
    },
    {
        "August": 67,
        "medium-critical": 3
    },
    {
        "August": 20,
        "medium-critical": 3
    },
    {
        "August": 66,
        "medium-critical": 3
    },
    {
        "August": 34,
        "medium-critical": 3
    },
    {
        "August": 60,
        "medium-critical": 3
    },
    {   
        "August": 75,
        "medium-critical": 3
    },
    {   
        "August": 49,
        "medium-critical": 3
    }
]

i have tried running the list "c" through a for loop but have not been able to get the assignment of the Keys to work correctly.

Something like this:

with open('C:\filepath\m.csv') as f:

    mydict=dict(csv.reader(f))
    mydict['mean b'] = b 
    mydict['mean a'] = a

    mydict = [mydict]
     print mydict
     new_dict = {}
     for i in mydict:
        new_dict[]

    with open('C:\filepath\jsonfile.json', 'w') as q:
        json.dump(mydict, q)

Is there a better way to get the desired output?

Upvotes: 0

Views: 75

Answers (1)

Anton Zuenko
Anton Zuenko

Reputation: 761

The solution:

import csv
import json

json.dump([{'September': k, 'medium-critical': v}
    for k, v in csv.reader(open('data.csv'))], open('data.json', 'w'))

Upvotes: 3

Related Questions