[英]Using python ijson to read a large json file with multiple json objects
我正在尝试使用ijson包解析一个大的(~100MB)json文件,它允许我以有效的方式与文件交互。 但是,在编写了这样的代码之后,
with open(filename, 'r') as f:
parser = ijson.parse(f)
for prefix, event, value in parser:
if prefix == "name":
print(value)
我发现代码只解析第一行,而不解析文件中的其余行!
以下是我的json文件的一部分:
{"name":"accelerator_pedal_position","value":0,"timestamp":1364323939.012000}
{"name":"engine_speed","value":772,"timestamp":1364323939.027000}
{"name":"vehicle_speed","value":0,"timestamp":1364323939.029000}
{"name":"accelerator_pedal_position","value":0,"timestamp":1364323939.035000}
在我看来,我认为ijson
只解析一个json对象。
有人可以建议如何解决这个问题?
不幸的是, ijson库(截至2018年3月的v2.3)不处理解析多个JSON对象。 它只能处理1个整体对象,如果你试图解析第二个对象,你会收到一个错误: "ijson.common.JSONError: Additional data"
。 在此处查看错误报告:
这是一个很大的限制。 但是,只要你有每个JSON对象后换行符(新行字符),可以单独分析每一行由行,像这样:
import io
import ijson
with open(filename, encoding="UTF-8") as json_file:
cursor = 0
for line_number, line in enumerate(json_file):
print ("Processing line", line_number + 1,"at cursor index:", cursor)
line_as_file = io.StringIO(line)
# Use a new parser for each line
json_parser = ijson.parse(line_as_file)
for prefix, type, value in json_parser:
print ("prefix=",prefix, "type=",type, "value=",value)
cursor += len(line)
您仍在流式传输文件,而不是将其完全加载到内存中,因此它可以处理大型JSON文件。 它还使用以下行线技术: 如何跳转到巨大文本文件中的特定行? 并使用enumerate()
from: 在'for'循环中访问索引?
由于提供的块看起来更像是一组构成独立JSON的行,因此应该对其进行解析:
# each JSON is small, there's no need in iterative processing
import json
with open(filename, 'r') as f:
for line in f:
data = json.loads(line)
# data[u'name'], data[u'engine_speed'], data[u'timestamp'] now
# contain correspoding values
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.