所以我有一些相当大的json编码文件.最小的是300MB,但这是迄今为止最小的.其余的是多GB,大约2GB到10GB +.
所以当我尝试使用Python加载文件时,我似乎耗尽了内存.我目前正在进行一些测试,看看处理这些东西需要多长时间才能看到从这里开始.这是我用来测试的代码:
from datetime import datetime
import json
print datetime.now()
f = open('file.json', 'r')
json.load(f)
f.close()
print datetime.now()
Run Code Online (Sandbox Code Playgroud)
毫不奇怪,Python给了我一个MemoryError.似乎json.load()调用json.loads(f.read()),它试图首先将整个文件转储到内存中,这显然不会起作用.
我能干什么方式解决这个问题吗?
我知道这是旧的,但我不认为这是重复的.答案是一样的,但问题是不同的.在"重复"中,问题是如何有效地读取大文件,而这个问题处理甚至根本不适合内存的文件.效率不是必需的.
我的代码是:data_review=pd.read_json('review.json')
我的数据review为fllow:
{
// string, 22 character unique review id
"review_id": "zdSx_SD6obEhz9VrW9uAWA",
// string, 22 character unique user id, maps to the user in user.json
"user_id": "Ha3iJu77CxlrFm-vQRs_8g",
// string, 22 character business id, maps to business in business.json
"business_id": "tnhfDv5Il8EaGSXZGiuQGg",
// integer, star rating
"stars": 4,
// string, date formatted YYYY-MM-DD
"date": "2016-03-09",
// string, the review itself
"text": "Great place to hang out after work: the prices are decent, and the ambience is fun. It's a …Run Code Online (Sandbox Code Playgroud) 当我将文件加载到json中时,pythons将内存使用量激增至约1.8GB,我似乎无法释放该内存.我整理了一个非常简单的测试用例:
with open("test_file.json", 'r') as f:
j = json.load(f)
Run Code Online (Sandbox Code Playgroud)
很抱歉我无法提供示例json文件,我的测试文件有很多敏感信息,但对于上下文,我正在处理240MB的文件.运行上面的2行后,我使用了前面提到的1.8GB内存.如果我然后做del j内存使用不会下降.如果我跟着它,gc.collect()它仍然不会下降.我甚至尝试卸载json模块并运行另一个gc.collect.
我正在尝试运行一些内存分析,但是堆积已经搅拌了100%的CPU大约一个小时,现在还没有产生任何输出.
有没有人有任何想法?我也尝试过使用cjson而不是打包的json模块.cjson使用的内存减少了约30%但显示完全相同的问题.
我在Ubuntu服务器11.10上运行Python 2.7.2.
我很高兴加载任何内存分析器,看看它是否比堆更好,并提供您认为必要的任何诊断.我正在寻找一个大型测试json文件,我可以为其他任何人提供它.
我一直在尝试将大量(> 800mb)数据写入JSON文件;我做了一些相当多的试验和错误来获得此代码:
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()
Run Code Online (Sandbox Code Playgroud)
运行它只需调用该函数write_to_cube({"some_data" = data})
现在这段代码的问题是,对于少量数据来说速度很快,但是当test.json文件超过 800mb 时就会出现问题。当我尝试更新或添加数据时,需要很长时间。
我知道有外部库,例如simplejson或jsonpickle,我不太确定如何使用它们。
还有其他办法解决这个问题吗?
更新:
我不确定这怎么可能是重复的,其他文章没有提到编写或更新大型 JSON 文件,而是只提到了解析。
有没有一种内存高效且快速的方法来在 python 中加载大 json 文件?
上述任何一个都不能重复解决这个问题。他们没有谈论任何有关写作或更新的事情。
我希望使用此处描述的装置加载初始数据
https://docs.djangoproject.com/en/dev/howto/initial-data/
对于小数据集来说这很容易。但是我希望加载一个无法装入内存的大 CSV。我该如何将其序列化为大型 JSON 格式?我是否必须通过手动编写开头“[”和结尾“]”来破解它,或者是否有更清洁的方法可以做到这一点?
我有一大堆JSON数据格式如下:
[
[{
"created_at": "2017-04-28T16:52:36Z",
"as_of": "2017-04-28T17:00:05Z",
"trends": [{
"url": "http://twitter.com/search?q=%23ChavezSigueCandanga",
"query": "%23ChavezSigueCandanga",
"tweet_volume": 44587,
"name": "#ChavezSigueCandanga",
"promoted_content": null
}, {
"url": "http://twitter.com/search?q=%2327Abr",
"query": "%2327Abr",
"tweet_volume": 79781,
"name": "#27Abr",
"promoted_content": null
}],
"locations": [{
"woeid": 395277,
"name": "Turmero"
}]
}],
[{
"created_at": "2017-04-28T16:57:35Z",
"as_of": "2017-04-28T17:00:03Z",
"trends": [{
"url": "http://twitter.com/search?q=%23fyrefestival",
"query": "%23fyrefestival",
"tweet_volume": 141385,
"name": "#fyrefestival",
"promoted_content": null
}, {
"url": "http://twitter.com/search?q=%23HotDocs17",
"query": "%23HotDocs17",
"tweet_volume": null,
"name": "#HotDocs17",
"promoted_content": null
}],
"locations": [{
"woeid": 9807,
"name": "Vancouver"
}]
}]
]...
Run Code Online (Sandbox Code Playgroud)
我编写了一个函数,将其格式化为采用以下形式的pandas数据框: …
我一直致力于将两个遗留数据库中的57k +记录精炼和重构为一个与Django兼容的实体.现在,当我完成后,我将其作为夹具倾倒,我试图在生产环境中加载它.
我的问题是这个过程在一段时间后被"杀死".我的过程是:
./manage.py syncdb --noinput
./manage.py loaddata core/fixtures/auth.json # just a default user
./manage.py migrate
Run Code Online (Sandbox Code Playgroud)
结果:
Running migrations for django_extensions: # custom apps migrate just fine
- Migrating forwards to 0001_empty.
> django_extensions:0001_empty
- Loading initial data for django_extensions.
Installed 0 object(s) from 0 fixture(s)
Running migrations for myotherapp:
- Migrating forwards to 0001_initial.
> myotherapp:0001_initial
- Loading initial data for myotherapp.
Installed 4 object(s) from 1 fixture(s) # my other app with a fixture migrates ok
Running migrations …Run Code Online (Sandbox Code Playgroud)