将文本数据转换为Json格式

时间:2017-10-17 18:11:40

标签: python json

我是Python的初学者。

我必须将文本数据转换为Json格式

以下是从下面的表结构中提取的示例文本数据:

(1 to 100) [position() mod 4 eq 0] [position() gt 23]

以下是代码:

Metric - Column 1 ,
Design Project - Column 2 ,
Design Target* - Column 3 ,
Median Property* - Column 4

Metric Design Project Design Target* Median Property*
Source EUI (kBtu/ft²) 3.1 Not Available 127.9
Site EUI (kBtu/ft²) 1.0 Not Available 40.7
Source Energy Use (kBtu) 314.0 Not Available 12,793.0
Site Energy Use (kBtu) 100.0 Not Available 4,074.2
Energy Cost ($) 2,000.00 Not Available 81,484.00
Total GHG Emissions (Metric Tons CO2e) 0.0 Not Available 0.5
ENERGY STAR score (1-100) Not Available 75 50

正确处理前两行数据的上述代码。

以下是代码的输出

import csv
import json

with open('output.txt', 'r') as csvfile: # Opens file
    filereader = csv.reader(csvfile, delimiter=' ')
    i = 0
    header = []
    out_data = []
    for row in filereader:
        row = [elem for elem in row if elem]
        if i == 0:
            i+=2
            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
        else:
            row[0:3] = [row[0]+" "+row[1]+" "+row[2]]  # Metric 
            if len(row) == 5:  # check conditions for better parse
                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)

如何将完整数据制作成json格式?

1 个答案:

答案 0 :(得分:0)

这是答案,

import csv
import json

with open('output.txt', 'r') as csvfile: # Opens file
    filereader = csv.reader(csvfile, delimiter=' ')
    i = 0
    header = []
    out_data = []
    for row in filereader:
        row = [elem for elem in row if elem]
        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
            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)

        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)