转储dict到json时有问题吗?

Cha*_*pps 1 python memory json dump simplejson

在这里,我想将一个"大"字典转储到json中,如下所示:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import simplejson as json

doc = {}
# appending the doc, so that the doc is more than 2G
.....

json_doc = json.dumps(doc)
Run Code Online (Sandbox Code Playgroud)

然后我收到以下错误消息:

  File "C:\Python27\lib\site-packages\simplejson\__init__.py", line 286, in dump
s
    return _default_encoder.encode(obj)
  File "C:\Python27\lib\site-packages\simplejson\encoder.py", line 228, in encod
e
    chunks = list(chunks)
MemoryError
Run Code Online (Sandbox Code Playgroud)

我该如何解决?谢谢!

jfs*_*jfs 5

如果内存太少,您可以尝试逐步将对象编码为json:

import json
import sys

d = dict.fromkeys(range(10))
for chunk in json.JSONEncoder().iterencode(d):
    print(chunk) # print each chunk on a newline for demonstration
Run Code Online (Sandbox Code Playgroud)

不要在字符串使用文件/套接字中累积输出并立即写入/发送块.

产量

{
"0"
: 
null
, 
"1"
: 
null
, 
"2"
: 
null
, 
"3"
: 
null
, 
"4"
: 
null
, 
"5"
: 
null
, 
"6"
: 
null
, 
"7"
: 
null
, 
"8"
: 
null
, 
"9"
: 
null
}
Run Code Online (Sandbox Code Playgroud)