python读json文件太大github_在Python中打开大型JSON文件

1586010002-jmsa.png

I have a 1.7 GB JSON file when I am trying to open with json.load() then it is giving memory error, How could read the json file in python?

My JSON file is a big array of objects containing specific keys.

EDIT: Well if it is just one big array of objects and it is known the structure of objects beforehand then there is no need to use tools we could read it line by line. A line will just contain one element of the array. I noticed that is the way json files are stored, for me it worked as just

>>>for line in open('file.json','r').readline():

... do something with(line)

解决方案

You want an incremental json parser like yajl and one of its python bindings. An incremental parser reads as little as possible from the input and invokes a callback when something meaningful is decoded. For example, to pull only numbers from a big json file:

class ContentHandler(YajlContentHandler):

def yajl_number(self, ctx, val):

list_of_numbers.append(float(val))

parser = YajlParser(ContentHandler())

parser.parse(some_file)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值