Python 将dict转储到json时出现问题?
在这里,我想将一个“大”dict转储到json中,如下所示:Python 将dict转储到json时出现问题?,python,json,memory,dump,simplejson,Python,Json,Memory,Dump,Simplejson,在这里,我想将一个“大”dict转储到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) 然后我得到了以下错误消息: File "C:\Python27\lib\site-packa
#!/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)
然后我得到了以下错误消息:
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
我怎样才能修好它?谢谢 如果内存不足,您可以尝试以增量方式将对象编码为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
不要将输出累积到字符串中,立即使用file/socket和write/send chunk
如果内存不足,您可以尝试以增量方式将对象编码为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
不要将输出累积到字符串中,立即使用file/socket和write/send chunk
我怎样才能修好它?这似乎是个愚蠢的问题。你试过什么?购买更多内存?制作一个更小的dict?没有太多的选择。你最初是如何制作出这么大的dict的?我如何修复它?这似乎是个愚蠢的问题。你试过什么?购买更多内存?制作一个更小的dict?没有太多的选择。你最初是怎么制作出这么大的dict的?