用Python解析JSON文件
问题描述:
所以我想用Python解析JSON文件。每次运行我的脚本时,我都会得到[]
的输出结果,我对此很困惑。这甚至是一个正确的方法来解析JSON在python中?用Python解析JSON文件
这里是我的代码:
import sys
import simplejson
import difflib
filename = sys.argv[1]
data = []
f = file('output.json', "r")
lines = f.readlines()
for line in lines:
try:
loadLines = simplejson.loads(line)
data.append(loadLines['executionTime'])
except ValueError:
pass
print data
答
我最好的猜测是对自己没有行是有效的JSON。这将导致每次抛出ValueError
,并且您将永远不会到达data.append(...)
,因为当时一直抛出异常。
如果整个文件是一个JSON数组是这样的:
[
{
"direction": "left",
"time": 1
},
{
"direction": "right",
"time": 2
}
]
然后,你可以简单地使用类似:
with open('output.json', 'r') as f:
data = json.load(f)
然而,如果它在的JSON物品一堆顶层,没有被封闭在一个JSON对象或数组中,像这样:
{
"direction": "left",
"time": 1
}
{
"direction": "right",
"time": 2
}
那么你将不得不去与一个不同的方法:逐项解码项目。不幸的是,我们不能流中的数据,所以我们首先必须加载在一旦所有的数据:
with open('output.json', 'r') as f:
json_data = f.read()
要分析一个项目,我们使用decode_raw
。这意味着我们需要一个JSONDecoder
:
decoder = json.JSONDecoder()
然后我们就一起去,剥离在串左侧的任何空白,检查,以确保我们仍然有项目,并分析项目:
while json_data.strip(): # while there's still non-whitespace...
# strip off whitespace on the left side of the string
json_data = json_data.lstrip()
# and parse an item, setting the new data to be whatever's left
item, json_data = decoder.parse_raw(json_data)
# ...and then append that item to our list
data.append(item)
如果你正在做大量的数据采集这样的,它可能是值得将其存储在数据库中。像SQLite这样简单的事情可以做得很好。一个数据库可以更容易地以有效的方式进行汇总统计。 (这就是它们的设计目的!)如果你经常使用这些数据,它可能会让访问数据的速度更快,而不是解析JSON。
@MattBall它与文件的大小无关。 – JBernardo
你们可以帮忙吗? – metersk
@Jernernardo确实,虽然标题暗示大小是问题。 –