python - 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 样本?
解决方案
将您的代码更改为:
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,直接追加即可。
推荐阅读
- spring-security - Zuul Gateway 通过 Eureka Server 启动时出现未经授权的异常
- python - ProcessPoolExecutor 以非直观的方式提高性能
- c++ - 为什么 mpfr_printf 与十六进制浮点数的 printf 不同(%a 转换说明符)?
- c++ - 在遍历处理程序向量时注册和取消注册处理程序
- amazon-lex - 在未识别意图后,Amazon lex 路由到 amazon connect 聊天
- image - 在markdown中包含图像时如何转义文件名中的空格?
- embedded - 在新项目中重新组织 STM32 代码示例会减慢代码速度
- deep-learning - YOLOv3 迁移学习性能比其官方权重差
- python - cx_Freeze 错误:基线图像目录不存在
- elasticsearch-aggregation - 在 `must` 中使用 `aggs` 的弹性查询