如何使用python将List转换为Json结构

时间:2017-11-20 06:06:48

标签: python json

我试图将列表转换为JSON结构。

这是我的清单,

[u'Metric Design Project Design Target* Median Property*', u'ENERGY STAR score (1-100) Not Available Not Available 50', u'Source EUI (kBtu/ft\xb2) Not Available 35.4 141.4', u'Site EUI (kBtu/ft\xb2) Not Available 15.8 63.1', u'Source Energy Use (kBtu) Not Available 3,536.0 14,144.1', u'Site Energy Use (kBtu) Not Available 1,578.7 6,314.9', u'Energy Cost ($) Not Available 34.61 138.44', u'Total GHG Emissions (Metric Tons CO2e) 0.0 0.2 0.6']

这是我试过的代码,

import json


filereader = [u'Metric Design Project Design Target* Median Property*', u'ENERGY STAR score (1-100) Not Available Not Available 50', u'Source EUI (kBtu/ft\xb2) Not Available 35.4 141.4', u'Site EUI (kBtu/ft\xb2) Not Available 15.8 63.1', u'Source Energy Use (kBtu) Not Available 3,536.0 14,144.1', u'Site Energy Use (kBtu) Not Available 1,578.7 6,314.9', u'Energy Cost ($) Not Available 34.61 138.44', u'Total GHG Emissions (Metric Tons CO2e) 0.0 0.2 0.6']

i = 0
header = []
out_data = []
for row in filereader:
    row = [elem for elem in row if elem]
    print row
    if i == 0:
        i+=1
        row[1:3] = [row[1]+" "+row[2]]  # Design Project key
        row[2:4] = [row[2]+" "+row[3]]  # Design Target key
        row[3:5] = [row[3]+" "+row[4]]  # Median Property*
        header = row

    elif len(row) == 8:

        row[0:4] = [row[0]+" "+row[1]+" "+row[2]+" "+row[3]]  # Metric value

        if row[1] == 'Not':
            row[1:3] = [row[1]+" "+row[2]]
        else:
            row[1:2] = [row[1]]  # Design Project Value
            row[2:4] = [row[2]+" "+row[3]] # Design Target

        _dict = {}
        for elem, header_elem in zip(row, header):
            _dict[header_elem] = elem
        out_data.append(_dict)


    elif len(row) == 7:
        #print len(row)
        row[0:3]=[row[0]+" "+row[1]+" "+row[2]]  # Metric Value

        if row[1] == 'Not':
            row[1:3] = [row[1]+" "+row[2]]


        _dict = {}
        for elem, header_elem in zip(row, header):
            _dict[header_elem] = elem
        out_data.append(_dict)

    elif len(row) == 9:

        if row[0] == 'ENERGY':
            row[0:4] = [row[0]+" "+row[1]+" "+row[2]+" "+row[3]]  # Metric value
        else:
            row[0:6] = [row[0]+" "+row[1]+" "+row[2]+" "+row[3]+" "+row[4]+" "+row[5]] # Metric Value

        if row[1] == 'Not':
            row[1:3] = [row[1]+" "+row[2]] # Design Project Value
        else:
            row[1:2] = [row[1]]  # Design Project Value

        if row[2] == 'Not':

            row[2:4] = [row[2]+" "+row[3]] # Design Target


        _dict = {}
        for elem, header_elem in zip(row, header):
            _dict[header_elem] = elem
        out_data.append(_dict)

    else:
        row[0:6] = [row[0]+" "+row[1]+" "+row[2]+" "+row[3]+" "+row[4]+" "+row[5]] # Metric Value
        row[1:2] = [row[1]]  # Design Project value
        row[2:4] = [row[2]+" "+row[3]] # Design Target
        _dict = {}
        for elem, header_elem in zip(row, header):
            _dict[header_elem] = elem
        out_data.append(_dict)

print json.dumps(out_data)

我的输出应该是,

[{"Metric": "ENERGY STAR score (1-100)", "Design Target*": "Not Available", "Median Property*": "50", "Design Project": "Not Available"}, {"Metric": "Source EUI (kBtu/ft\u00b2)", "Design Target*": "35.4", "Median Property*": "141.4", "Design Project": "Not Available"}, {"Metric": "Site EUI (kBtu/ft\u00b2)", "Design Target*": "15.8", "Median Property*": "63.1", "Design Project": "Not Available"}, {"Metric": "Source Energy Use (kBtu)", "Design Target*": "3,536.0", "Median Property*": "14,144.1", "Design Project": "Not Available"}, {"Metric": "Site Energy Use (kBtu)", "Design Target*": "1,578.7", "Median Property*": "6,314.9", "Design Project": "Not Available"}, {"Metric": "Energy Cost ($)", "Design Target*": "34.61", "Median Property*": "138.44", "Design Project": "Not Available"}, {"Metric": "Total GHG Emissions (Metric Tons CO2e)", "Design Target*": "0.2", "Median Property*": "0.6", "Design Project": "0.0"}]

0 个答案:

没有答案