如何使用python从json在csv中添加日期时间值?

时间:2017-07-06 08:19:06

标签: python arrays json csv

我正在尝试使用python将具有多个数组和对象jsons的嵌套json转换为CSV文件。我尝试了很多方法,不知怎的,我得到的csvfile只有1个数组。我想打印许多内部具有唯一值的过滤器,因此我无法使用成本打印第二个数组,我的样本json如下:

SAMPLE JSON:

     {
    "breakdown":[
     {
     "filter":"tags",
     "cost":5517.734,
     "values":[
        {
           "count":13,
           "subvalues":[
              {
                 "count":1,
                 "subvalue":"BizOps-VM-20",
                 "cost":227.576
                  {
                 "count":4,
                 "subvalue":"<nulltagvalue>",
                 "cost":70.358
              },
                 "subvalues":[
              {
                 "count":2,
                 "subvalue":"cb",
                 "cost":4.584
              }
           ],
           "cost":4.584,
           "value":"TagNewForTestw"
           }
           ]   
           },
             {
        "filter":"account",
        "cost":2954.905,
        "values":[
        {
           "count":86,
           "cost":1613.508,
           "value":"628455167342"
         },]
         },
       "time":[
        {
        "cost":112.793,
       "time":"2017-05-16"
       },
      {
      "cost":112.748,
     "time":"2017-05-17"
        },
        {
       "cost":112.739,
       "time":"2017-05-18"
       },  
      ]

现在我在这里有时间数组,其中时间和成本在json中我希望使用python代码在csv中打印,但在尝试打印时,我正在使用######格式代替时间值。还有一件事是,即使在阵列时间结束后,我也会重复打印时间值。

我的python代码:

      import json
      import csv
      import time
      f = open('cost_drilldown_data.json')            
      data = json.load(f)
      s=csv.writer(open('cd13old.csv','w'))
      s.writerow(["filter","cost","value","cost","subvalue","cost"])
      i=0
      for breakdown in data['breakdown']:
           for values in breakdown['values']:
              if 'subvalues' in values:
                  for subvalues in values['subvalues']:
                      for time in data['time']:                     
                        s.writerow([breakdown['filter'],breakdown["cost"],
                       values['value'],values['cost'],subvalues["subvalue"],
                       subvalues["cost"],time['time'],time['cost']])
              else :       
      s.writerow([breakdown['filter'],"","",values['value'],values['cost']])

输出结果为:

filter,cost,value,cost,subvalue,cost,time,cost

标签,5517.734,名称,462.62,BizOps-VM-20,227.576,2017-05-16,112.793

标签,5517.734,名称,462.62,BizOps-VM-20,227.576,2017-05-17,112.748

标签,5517.734,名称,462.62,BizOps-VM-20,227.576,2017-05-18,112.739

标签,5517.734,名称,462.62,BizOps-VM-20,227.576,2017-05-19,107.165

标签,5517.734,名称,462.62,BizOps-VM-20,227.576,2017-05-20,89.988

标签,5517.734,名称,462.62,BizOps-VM-20,227.576,2017-05-21,88.773

标签,5517.734,名称,462.62,BizOps-VM-20,227.576,2017-05-22,96.591

标签,5517.734,名称,462.62,BizOps-VM-20,227.576,2017-05-23,104.809

标签,5517.734,名称,462.62,BizOps-VM-20,227.576,2017-05-24,104.471

标签,5517.734,名称,462.62,BizOps-VM-20,227.576,2017-05-25,104.472

标签,5517.734,名称,462.62,BizOps-VM-20,227.576,2017-05-26,103.536

标签,5517.734,名称,462.62,BizOps-VM-20,227.576,2017-05-27,83.205

标签,5517.734,名称,462.62,BizOps-VM-20,227.576,2017-05-28,83.103

标签,5517.734,名称,462.62,BizOps-VM-20,227.576,2017-05-29,83.111

标签,5517.734,名称,462.62,BizOps-VM-20,227.576,2017-05-30,84.555

标签,5517.734,名称,462.62,BizOps-VM-20,227.576,2017-05-31,86.986

标签,5517.734,名称,462.62,BizOps-VM-20,227.576,2017-06-01,103.024

标签,5517.734,名称,462.62,BizOps-VM-20,227.576,2017-06-02,100.905

标签,5517.734,名称,462.62,BizOps-VM-20,227.576,2017-06-03,102.062

标签,5517.734,名称,462.62,BizOps-VM-20,227.576,2017-06-04,105.243

标签,5517.734,名称,462.62,BizOps-VM-20,227.576,2017-06-05,105.134

标签,5517.734,名称,462.62,BizOps-VM-20,227.576,2017-06-06,111.764

标签,5517.734,名称,462.62,BizOps-VM-20,227.576,2017-06-07,107.455

标签,5517.734,名称,462.62,BizOps-VM-20,227.576,2017-06-08,107.62

标签,5517.734,名称,462.62,BizOps-VM-20,227.576,2017-06-09,107.61

标签,5517.734,名称,462.62,BizOps-VM-20,227.576,2017-06-10,102.53

标签,5517.734,名称,462.62,BizOps-VM-20,227.576,2017-06-11,102.282

标签,5517.734,名称,462.62,BizOps-VM-20,227.576,2017-06-12,105.52

标签,5517.734,名称,462.62,BizOps-VM-20,227.576,2017-06-13,93.617

标签,5517.734,名称,462.62,BizOps-VM-20,227.576,2017-06-14,41.094

标签,5517.734,名称,462.62,70.358,2017-05-16,112.793

标签,5517.734,名称,462.62,70.358,2017-05-17,112.748

标签,5517.734,名称,462.62,70.358,2017-05-18,112.739

它一次又一次地打印值,我尝试使用迭代但不是abe来停止值的重复。所以任何人都可以在这里帮助或指导我,我被困住了

预期的出口是:

过滤器,成本,值,成本,子值,成本,时间,成本

标签,5517.734,名称,462.62,BizOps-VM-20,227.576,2017-05-16,112.793 标签,5517.734,名称,462.62,70.358,2017-05-16,112.793 标记,5517.734,名称,462.62,BizOps01,60.188,2017-06-10,102.53

1 个答案:

答案 0 :(得分:0)

with open('cd13old.csv','w') as csvfile:
    s=csv.writer(csvfile)
    s.writerow(["filter","cost","value","cost","subvalue","cost"])
    for breakdown in data['breakdown']:
        for values in breakdown['values']:
            if 'subvalues' in values:
                for subvalues in values['subvalues']:
                    s.writerow([breakdown['filter'],values['value'],values['cost'],subvalues["subvalue"],subvalues["cost"]])
            else:
                s.writerow([breakdown['filter'],values['value'],values['cost'],"",""])