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

时间:2022-02-03 20:20:06

I have data in a JSON object with a key:value as shown below in Python. There are two records having same ID 13 for the Hari and 16 for the Liz.

我在JSON对象中有数据,其键值为:如下面的Python所示。有两个记录具有相同的ID 13用于Hari,16个用于Liz。

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()

I want to reformat JSON data into list without a key and merge duplicated ID as a list of list. The record with same ID's will be mapped into list of list as shown below. How can I achieve this?

我想在没有密钥的情况下将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"]]]

Iterate over the new list data and write each list data as a row in excel (xlwt) file

迭代新的列表数据并将每个列表数据作为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 个解决方案

#1


2  

To check the same ID you need to store data in a dict

要检查相同的ID,您需要将数据存储在dict中

ret_dict = {}

But dict isn't ordered. If you want to hold your order, you can you OrderedDict

但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())

For me ret_dict.values() is already a good result:

对我来说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']]]

But for exactly what you want, you need to build a new list from the values of the last dict:

但是对于你想要的,你需要从最后一个dict的值构建一个新的列表:

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

itervalues() to get the iterator of values instead of a list like values()
Output:

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']]]

#1


2  

To check the same ID you need to store data in a dict

要检查相同的ID,您需要将数据存储在dict中

ret_dict = {}

But dict isn't ordered. If you want to hold your order, you can you OrderedDict

但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())

For me ret_dict.values() is already a good result:

对我来说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']]]

But for exactly what you want, you need to build a new list from the values of the last dict:

但是对于你想要的,你需要从最后一个dict的值构建一个新的列表:

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

itervalues() to get the iterator of values instead of a list like values()
Output:

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']]]