首页 > 解决方案 > 根据python中的元素个数,将[]开头的数组类型的json日志拆分成多个数组

问题描述

我正在尝试通过读取我的 JSON 日志文件来创建多个文件,并且必须为我的 JSON 数组中的每个元素创建单独的文件。任何帮助,将不胜感激。

方法:能够使用读取json日志文件json.load()并读取json数组中的元素数量,然后将前5个元素作为json数组复制到file1中,然后将5个元素作为json数组复制到第二个文件中,依此类推直到长度我的 json 日志文件数组。更新:我让它工作,直到将列表/数组拆分为 10 个单独的列表/数组,直到文件结束。现在必须将这些列表写入单独的 json.gz 文件并上传到 s3。

import json


json_data = json.dumps([
  {
    "a": "1",
    "b": "2"
  },
  {
    "d": "3"
  },
  {
    "d": "4"
  },
  {
    "e": "5"
  },
  {
    "e": "6"
  },
  {
    "e": "7"
  },
  {
    "e": "8"
  },
  {
    "e": "9"
  },
  {
    "e": "5"
  },
  {
    "e": "10"
  },
  {
    "e": "11"
  },
  {
    "e": "12"
  },
  {
    "s": "y"
  },
  {
    "s": "y"
  },
  {
    "s": "y"
  },
  {
    "s": "y"
  },
  {
    "s": "y"
  },
  {
    "s": "y"
  },
  {
    "s": "y"
  },
  {
    "s": "y"
  },
  {
    "s": "y"
  },
  {
    "s": "y"
  },
  {
    "s": "y"
  },
  {
    "s": "y"
  },
  {
    "s": "y"
  },
  {
    "s": "y"
  },
  {
    "s": "y"
  },
  {
    "abc": [
      {
        "te": "tre"
      }
    ]
  }
])

item_dict = json.loads(json_data)
lens = len(item_dict)
first = 0
last = 10
b = item_dict[first:last]
#print (item_dict[0:2])
print (lens)
for i in range(lens):
    if last < lens:
       n = 0
       res = item_dict[first:last]
       print(range(lens))
       print(res)
       first = last 
       last = first + 10
       i = i + 1 
       print(first)
       print(last)
       print(i)
       if last > lens:

          diff = last-lens
          last = last-diff 

          print("second" + str(first))
          print("second" + str(last))
          ins = item_dict[first:last] 
          print(ins)```

my json log file:

```[
  {
    "id": 1000,
    "type": "BigBox",
    "name": "Mall of America",
    "address": "340 W Market",
    "address2": "",
    "city": "Bloomington",
    "state": "MN",
    "zip": "55425",
    "location": {
      "lat": 44.85466,
      "lon": -93.24565
    },
    "hours": "Mon: 10-9:30; Tue: 10-9:30; Wed: 10-9:30; Thurs: 10-9:30; Fri: 10-9:30; Sat: 10-9:30; Sun: 11-7",
    "services": [
      "Geek Squad Services",
      "Best Buy Mobile",
      "Best Buy For Business"
    ]
  },
  {
    "id": 1002,
    "type": "BigBox",
    "name": "Tempe Marketplace",
    "address": "1900 E Rio Salado Pkwy",
    "address2": "",
    "city": "Tempe",
    "state": "AZ",
    "zip": "85281",
    "location": {
      "lat": 33.430729,
      "lon": -111.89966
    },
    "hours": "Mon: 10-9; Tue: 10-9; Wed: 10-9; Thurs: 10-9; Fri: 10-10; Sat: 10-10; Sun: 10-8",
    "services": [
      "Windows Store",
      "Geek Squad Services",
      "Best Buy Mobile",
      "Best Buy For Business"
    ]}
  ]```

标签: jsonpython-3.xlambdasplit

解决方案


推荐阅读