Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将dict转储到json时出现问题?_Python_Json_Memory_Dump_Simplejson - Fatal编程技术网

Python 将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

在这里,我想将一个“大”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-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的?