如何写大的JSON数据? [英] How to write large JSON data?
问题描述
我一直试图将大量(> 800mb)的数据写入JSON文件;为了获得此代码,我进行了大量的试验和错误:
I have been trying to write large amount (>800mb) of data to JSON file; I did some fair amount of trial and error to get this code:
def write_to_cube(data):
with open('test.json') as file1:
temp_data = json.load(file1)
temp_data.update(data)
file1.close()
with open('test.json', 'w') as f:
json.dump(temp_data, f)
f.close()
要运行它,只需调用函数write_to_cube({"some_data" = data})
to run it just call the function write_to_cube({"some_data" = data})
现在,此代码的问题在于处理少量数据的速度很快,但是问题出在test.json
文件中的文件超过800mb时.当我尝试更新或向其中添加数据时,需要花费一些时间.
Now the problem with this code is that it's fast for the small amount of data, but the problem comes when test.json
file has more than 800mb in it. When I try to update or add data to it, it takes ages.
我知道有外部库,例如simplejson
或jsonpickle
,但我不确定如何使用它们.
I know there are external libraries such as simplejson
or jsonpickle
, I am not pretty sure on how to use them.
还有其他解决方法吗?
更新:
我不确定这怎么可能是重复的,其他文章没有提及编写或更新大型JSON文件,而只是谈论解析.
I am not sure how this can be a duplicate, other articles say nothing about writing or updating a large JSON file, rather they say only about parsing.
是否有一种内存高效且快速的方式可以在python中加载大型json文件?
以上方法均不能解决该问题.他们没有说写或更新.
None of the above resolve this question a duplicate. They don't say anything about writing or update.
推荐答案
所以问题是您的操作很长.这是我通常会采用的几种方法:
So the problem is that you have a long operation. Here are a few approaches that I usually do:
- 优化操作:这很少起作用.我不建议使用任何精湛的库来更快地解析json
- 更改逻辑:如果目的是保存和加载数据,则可能要尝试其他操作,例如将对象存储到二进制文件中.
- 线程和回调,或某些Web框架中的延迟对象.对于Web应用程序,有时操作需要的时间比请求等待的时间长,我们可以在后台进行操作(某些情况是:压缩文件,然后将zip发送到用户的电子邮件,通过调用另一个第三方的api发送SMS. ..)
这篇关于如何写大的JSON数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!