Python - 将JSON对象数据转换为List

时间:2016-05-18 13:20:22

标签: python json list dictionary

我在 JSON 对象中有key:value的数据,如 Python 中所示。对于 Hari ,有两个记录具有相同的 ID 13 ,对于 Liz 16 >

from collections import defaultdict
from itertools import *
from operator import itemgetter

data = [
  {
    "fname": "Abc",
    "lname": "xyz",
    "id": 15,
    "club": "-",
    "date": "-"
  },
  {
    "fname": "Hari",
    "lname": "Lee",
    "id": 13,
    "club": "Manutd",
    "date": "2016-03-20T22:00:00.000Z"
  },
  {
    "fname": "David",
    "lname": "James",
    "id": 14,
    "club": "Barca",
    "date": "-"
  },
  {
    "fname": "Hari",
    "lname": "Lee",
    "id": 13,
    "club": "Chelsea",
    "date": "2012-03-20T22:00:00.000Z"
  },
  {
    "fname": "Liz",
    "lname": "Kiz",
    "id": 16,
    "club": "-",
    "date": "-"
  },
  {
    "fname": "Liz",
    "lname": "Kiz",
    "id": 16,
    "club": "Falkon",
    "date": "2014-03-20T22:00:00.000Z"
  }
]

newdata = []
#for item, value in enumerate(data):
  #for i,v in value.iteritems():
    #print value['id']
    #print value[i]
    #print i,v
    #newdata.append()

我想将JSON数据重新格式化为不带的列表,并将重复的 ID 合并为列表列表。具有相同 ID 的记录将映射到列表列表,如下所示。我怎样才能做到这一点?

newdata = [[["Hari", "Lee", "Manutd", "2016-03-20T22:00:00.000Z"], ["Hari", "Lee", "Chelsea", "2012-03-20T22:00:00.000Z"]], 
    ["David", "James", "Barca", "-"], ["Abc", "xyz", "-" "-"], [["Liz", "Kiz", "-", "-"], ["Liz", "Kiz", "Falkon", "2014-03-20T22:00:00.000Z"]]]

迭代新的列表数据并将每个列表数据作为excel(xlwt)文件中的一行写入

for i1, v1 in enumerate(newdata):
  for i2,v2 in enumerate(v1):
    if(type(v2) is str):
      print v2
    else:
      for i3,v3 in enumerate(v2):
        print v3

1 个答案:

答案 0 :(得分:2)

要检查您需要在dict

中存储数据所需的相同ID
ret_dict = {}

dict没有订购。如果您想保留订单,可以OrderedDict

from collections import OrderedDict
ret_dict = OrderedDict()
for element in data:
    # To remove 'id' from the dict 'element' use element.pop('id')
    # element.pop('id') return the value of id
    ret_dict.setdefault(element.pop('id'), []).append(element.values())

对我而言ret_dict.values()已经是一个好结果:

>>> print ret_dict.values()
[[['xyz', '-', '-', 'Abc']], [['Lee', 'Manutd', '2016-03-20T22:00:00.000Z', 'Hari'], ['Lee', 'Chelsea', '2012-03-20T22:00:00.000Z', 'Hari']], [['James', 'Barca', '-', 'David']], [['Kiz', '-', '-', 'Liz'], ['Kiz', 'Falkon', '2014-03-20T22:00:00.000Z', 'Liz']]]

但是对于你想要的,你需要从最后一个词典的values建立一个新的列表:

ret_list = [e[0] if len(e) == 1 else e for e in ret_dict.itervalues()]

itervalues()获取值的迭代器而不是像values() 这样的列表 输出:

>>> print ret_list
[['xyz', '-', '-', 'Abc'], [['Lee', 'Manutd', '2016-03-20T22:00:00.000Z', 'Hari'], ['Lee', 'Chelsea', '2012-03-20T22:00:00.000Z', 'Hari']], ['James', 'Barca', '-', 'David'], [['Kiz', '-', '-', 'Liz'], ['Kiz', 'Falkon', '2014-03-20T22:00:00.000Z', 'Liz']]]