2013-06-26 175 views
0

所以我想用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 
+1

@MattBall它与文件的大小无关。 – JBernardo

+0

你们可以帮忙吗? – metersk

+0

@Jernernardo确实,虽然标题暗示大小是问题。 –

回答

7

我最好的猜测是对自己没有行是有效的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。

+0

你能解释一下这是什么意思吗? “但是,如果它是顶层的一堆JSON项目,而不是包含在JSON对象或数组中,那么您将不得不采用不同的方法。” 我制作了JSON文件,输出这个链接bikenyc.com/stations/json每分钟用一个python脚本和终端 – metersk

+0

@ user1887261:我已经添加了一个示例来显示不同之处。请注意,第一个被'['和']'包围,并且用逗号分隔各个项目,而后者没有。 – icktoofay

+0

啊,好吧,编辑现在更有意义!谢谢。所以我的JSON被格式化为最底层的代码。我在哪里开始制定基于此的不同方法? – metersk