首页 > 解决方案 > Python - 附加 json 文件

问题描述

我正在尝试将文件夹中的 json 文件附加到变量中,以便稍后将其解析出来。这是我的代码:

    # Importing dependencies
import os
import shutil
import glob
from zipfile import ZipFile
from datetime import datetime
import zipfile
import json
from pandas.io.json import json_normalize
import urllib
import sqlalchemy as sa

# Define the folder sources and destinations
MainDir = 'C:/Test/'
LoadingDir =  'C:/Test/Loading/'
ArchiveDir = 'C:/Test/Archive/'

glob_data = []
# Look for all json files in directory
for file in glob.glob(LoadingDir + '*.json'):
    with open(file) as json_file:
        # Load each json file and append it
        data = json.load(json_file)
        i = 0
        while i < len(data):
            glob_data.append(data[i])
            i += 1
with open(LoadingDir + 'Combined.json', 'w') as f:
    json.dump(glob_data, f, indent=4)
# Load Json file for parsing
file = open(LoadingDir + 'Combined.json')
data = json.load(file)
# Parsing of data
df = json_normalize(data,meta=['timestamp'])
df.to_csv(LoadingDir + "Combined.csv",sep=',', encoding='utf-8')
try:
    df.to_csv(LoadingDir + "Combined.csv",sep=',', encoding='utf-8')
except:
    pass

当我尝试运行它时,我在下面收到此消息:

---------------------------------------------------------------------------

KeyError                                  Traceback (most recent call last)

<ipython-input-1-ea0f48aa463e> in <module>
     24         i = 0
     25         while i < len(data):
---> 26             glob_data.append(data[i])
     27             i += 1
     28 with open(LoadingDir + 'Combined.json', 'w') as f:

KeyError: 0

这是我的 Json 文件的示例:

  {
  "sensor-time" : {
    "timezone" : "America/Los_Angeles",
    "time" : "2019-11-05T14:18:36-08:00"
  },
  "status" : {
    "code" : "OK"
  },
  "content" : {
    "element" : [ {
      "element-id" : 0,
      "element-name" : "Line 0",
      "sensor-type" : "SINGLE_SENSOR",
      "data-type" : "LINE",
      "from" : "2019-11-01T00:00:00-07:00",
      "to" : "2019-11-05T15:00:00-08:00",
      "resolution" : "ONE_HOUR",
      "measurement" : [ {
        "from" : "2019-11-01T00:00:00-07:00",
        "to" : "2019-11-01T01:00:00-07:00",
        "value" : [ {
          "value" : 0,
          "label" : "fw"
        }, {
          "value" : 0,
          "label" : "bw"
        } ]
      }, {
        "from" : "2019-11-01T01:00:00-07:00",
        "to" : "2019-11-01T02:00:00-07:00",
        "value" : [ {
          "value" : 0,
          "label" : "fw"
        }, {
          "value" : 0,
          "label" : "bw"
        } ]
      }, {
        "from" : "2019-11-01T02:00:00-07:00",
        "to" : "2019-11-01T03:00:00-07:00",
        "value" : [ {
          "value" : 0,
          "label" : "fw"
        }, {
          "value" : 0,
          "label" : "bw"
        } ]
      },

所以我注意到这个 json 文件不是以 [ 开头的,这意味着它不是字典列表。但是当我有以 [ 开头的 json 时,我的代码确实有效。我如何调整它以适用于这个 json 样本?

标签: pythonjsonpython-3.x

解决方案


将您的代码更改为:

import os
import shutil
import glob
from zipfile import ZipFile
from datetime import datetime
import zipfile
import json
from pandas.io.json import json_normalize
import urllib
import sqlalchemy as sa

# Define the folder sources and destinations
MainDir = 'C:/Test/'
LoadingDir =  'C:/Test/Loading/'
ArchiveDir = 'C:/Test/Archive/'

glob_data = []
# Look for all json files in directory
for file in glob.glob(LoadingDir + '*.json'):
    with open(file) as json_file:
        # Load each json file and append it
        data = json.load(json_file)
        glob_data.append(data)
with open(LoadingDir + 'Combined.json', 'w') as f:
    json.dump(glob_data, f, indent=4)
# Load Json file for parsing
file = open(LoadingDir + 'Combined.json')
data = json.load(file)
# Parsing of data
df = json_normalize(data,meta=['timestamp'])
df.to_csv(LoadingDir + "Combined.csv",sep=',', encoding='utf-8')
try:
    df.to_csv(LoadingDir + "Combined.csv",sep=',', encoding='utf-8')
except:
    pass

您不需要遍历 json.load() 返回的返回值,它已经被解析并转换为 dict,直接追加即可。


推荐阅读