是否有一种内存高效且快速的方式来加载大型 JSON 文件? [英] Is there a memory efficient and fast way to load big JSON files?

查看:27
本文介绍了是否有一种内存高效且快速的方式来加载大型 JSON 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些 500MB 的 json 文件.如果我使用琐碎"json.load() 一次性加载所有内容,会消耗大量内存.

I have some json files with 500MB. If I use the "trivial" json.load() to load its content all at once, it will consume a lot of memory.

有没有办法部分读取文件?如果它是一个文本,行分隔文件,我将能够遍历这些行.我正在寻找与它的类比.

Is there a way to read partially the file? If it was a text, line delimited file, I would be able to iterate over the lines. I am looking for analogy to it.

推荐答案

更新

请参阅其他答案以获取建议.

Update

See the other answers for advice.

简答:没有.

正确划分 json 文件需要对 json 对象图的深入了解才能正确.

Properly dividing a json file would take intimate knowledge of the json object graph to get right.

但是,如果您有这些知识,那么您可以实现一个类似文件的对象,该对象包装 json 文件并吐出适当的块.

However, if you have this knowledge, then you could implement a file-like object that wraps the json file and spits out proper chunks.

例如,如果您知道您的 json 文件是单个对象数组,您可以创建一个生成器来包装 json 文件并返回该数组的块.

For instance, if you know that your json file is a single array of objects, you could create a generator that wraps the json file and returns chunks of the array.

您必须进行一些字符串内容解析才能正确地对 json 文件进行分块.

You would have to do some string content parsing to get the chunking of the json file right.

我不知道是什么生成了您的 json 内容.如果可能,我会考虑生成多个可管理的文件,而不是一个大文件.

I don't know what generates your json content. If possible, I would consider generating a number of managable files, instead of one huge file.

这篇关于是否有一种内存高效且快速的方式来加载大型 JSON 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆