使用Python将一个JSON对象/字典变成两个

时间:2018-06-21 18:19:06

标签: python json

我的JSON数据格式如下。

  {
    "website": "http://www.zebrawebworks.com/zebra/bluetavern/day.cfm?&year=2018&month=6&day=29", 
    "date": "2018-06-29", 
    "headliner": [
      "Delta Ringnecks", 
      "Flathead String Band"
    ], 
    "data": [
      "4:00 PM", 
      "FEE:  $0", 
      "Jug Band Music", 
      "8:00 PM", 
      "FEE:  $5", 
      "Old Time Fiddle & Banjoby some young turks!"
    ]
  }

我正在for concert in data:循环中处理一系列看起来像这样的项目。在有两个音乐会的日期上,我需要创建一个新的Python词典,以便每个音乐会都在自己的字典中,如下所示:

  {
    "website": "http://www.zebrawebworks.com/zebra/bluetavern/day.cfm?&year=2018&month=6&day=29", 
    "date": "2018-06-29", 
    "headliner": "Delta Ringnecks",  
    "data": [
      "4:00 PM", 
      "FEE:  $0", 
      "Jug Band Music", 
    ]
  },
  {
    "website": "http://www.zebrawebworks.com/zebra/bluetavern/day.cfm?&year=2018&month=6&day=29", 
    "date": "2018-06-29", 
    "headliner": "Flathead String Band"
    "data": [
      "8:00 PM", 
      "FEE:  $5", 
      "Old Time Fiddle & Banjoby some young turks!"
    ]
  }

有推荐的方法吗?我不能在for循环中更改数据,对吗?因为那样会破坏我的迭代。

我可以将其附加到data的末尾,以便for循环覆盖新的字典(在分离所有内容后我仍然需要解析一些数据)?

还是我应该用分割日创建一个新词典,删除一日两场音乐会的对象,然后合并剩下的字典?

我希望这是足够的信息,并且我不会在术语中混淆太多。我是JSON Python模块的新手,并且一直在努力有效地解决这个问题。谢谢。

3 个答案:

答案 0 :(得分:1)

我建议您创建dict的副本并将特定数据存储在每个副本中。例如:

result = [] 

for pos in range(0, len(original_dict['headliner'])):
     new_dict = original_dict.copy()
     new_dict['data'] = original_dict['data'][pos*3:(pos+1)*3]
     new_dict['headliner'] = original_dict['headliner'][pos]
     result.append(new_dict)

print(result)

答案 1 :(得分:1)

您可以使用grouper文档中的itertools惯用法来获得一个非常干净的版本:

In [42]: new_list = [{'website': d['website'], 'date': d['date'], 'headliner': headliner, 'data': list(datarow)}
    ...:             for headliner, datarow in zip(d['headliner'], grouper(d['data'], 3))]
    ...:

In [43]: new_list
Out[43]:
[{'website': 'http://www.zebrawebworks.com/zebra/bluetavern/day.cfm?&year=2018&month=6&day=29',
  'date': '2018-06-29',
  'headliner': 'Delta Ringnecks',
  'data': ['4:00 PM', 'FEE:  $0', 'Jug Band Music']},
 {'website': 'http://www.zebrawebworks.com/zebra/bluetavern/day.cfm?&year=2018&month=6&day=29',
  'date': '2018-06-29',
  'headliner': 'Flathead String Band',
  'data': ['8:00 PM',
   'FEE:  $5',
   'Old Time Fiddle & Banjoby some young turks!']}]

答案 2 :(得分:0)

这是我在上面nosklo的帮助下想出的解决方案。希望它对将来有类似问题的人有所帮助。

new_concerts = []
    for concert in blue_data:

        if len(concert['headliner']) == 2:
            new_concert = concert.copy()
            new_concert['headliner'] = str(concert['headliner'][1])
            concert['headliner'] = str(concert['headliner'][0])
            mid = len(concert['data']) / 2
            new_concert['data'] = concert['data'][mid:]
            concert['data'] = concert['data'][0:mid]
            new_concerts.append(new_concert)

    blue_data = blue_data + new_concerts