2017-10-17 166 views
0

我是Python的初学者。将文本数据转换为Json格式

我的文本数据转换成JSON格式

这里是从下面的表结构中提取的样本的文本数据:

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) 

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

这里是代码的输出,

[{"Metric": "Source EUI (kBtu/ft\u00b2)", "Design Target*": "Not Available", "Median Property*": "127.9", "Design Project": "3.1"}, {"Metric": "Site EUI (kBtu/ft\u00b2)", "Design Target*": "Not Available", "Median Property*": "40.7", "Design Project": "1.0"}] 

我怎样才能让完整的数据转换成JSON格式?

+0

您的数据并不一致。并且'row = [elem for elem in row if elem]'对于第三数据行将具有不同数量的元素,而对于第二数据行将具有不同数量的元素。这就是为什么它不起作用。您可能需要在多种情况下分开提取,例如当len(row)== 5'做一次转换,并且当'len(row)== 6'做其他转换时 – PerunSS

+0

但是如果我写IF ELSe条件它的抛出错误@PerunSS – venkat

+0

你可以添加你试过的? – PerunSS

回答

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)