0

你能给出一些想法如何做这个集合。问题是这样的:我让 JSON 假设以下内容

[{
    "pk": 1,
    "model": "store.book",
    "fields": {
        "name": "Mostly Harmless",
        "author": ["Douglas", "Adams"]
    }
}]

然后解压一个文件我保存数据并关闭文件,下次(这是一个循环)再次像JSON一样再次接收,例如,如下

[{
    "pk": 2,
    "model": "store.book",
    "fields": {
        "name": "Henry",
        "author": ["Hans"]
    }
}]

第二个 JSON 必须放入它所在的文件和第一个文件中。怎么办的问题来了。在这个阶段,我是按照以下方式进行的,删除括号并放入逗号。有没有更聪明更好的方法来完成这项工作?

创建 JSON-Serializing Django 对象的使用。如果您能分享他们的想法,我将不胜感激。

PS:使用最少的内存很重要。假设文件大约 50-60 GB 并且在内存中最多可容纳大约 1 GB

4

3 回答 3

0

您不需要解析 JSON,因为您只是存储它。以下(a)创建一个文件,(b)在每个循环中将文本附加到文件中。

from os.path import getsize

def init(filename):
    """
    Creates a new file and sets its content to "[]".
    """
    with open(filename, 'w') as f:
        f.write("[]")
        f.close()

def append(filename, text):
    """
    Appends a JSON to a file that has been initialised with `init`.
    """
    length = getsize(filename) #Figure out the number of characters in the file
    with open(filename, 'r+') as f:
        f.seek(length - 1) #Go to the end of the file
        if length > 2: #Insert a delimiter if this is not the first JSON
            f.write(",\n")
        f.write(text[1:-1]) #Append the JSON
        f.write("]") #Write a closing bracket
        f.close()

filename = "temp.txt"
init(filename)

while mycondition:
    append(filename, getjson())

如果您不必在每个循环后保存 JSON,您可以执行以下操作

jsons = []
while mycondition:
    jsons.append(getjson()[1:-1])

with open("temp.txt", "w") as f:
    f.write("[")
    f.write(",".join(jsons))
    f.write("]")
    f.close()
于 2014-03-28T12:41:04.703 回答
0

您必须将数据转换为 JSON 并将其存储到文件中。然后再次从文件中读取并将新数据附加到对象并再次将其保存到文件中。以下是一些可能对您有用的代码:

使用 JSON。文档位于 - http://docs.python.org/2/library/json.html

第一次写入文件时,可以使用以下内容:

>>> import json
>>> fileW = open("filename.txt","w")
>>> json1 = [{
...     "pk": 1,
...     "model": "store.book",
...     "fields": {
...         "name": "Mostly Harmless",
...         "author": ["Douglas", "Adams"]
...     }
... }]
>>> json.dump(json1, fileW)
>>> fileW.close()

以下代码可用于循环读取文件并向其中添加数据。

>>> fileLoop = open("filename.txt","r+")
>>> jsonFromFile = json.load(fileLoop)
>>> jsonFromFile
[{u'pk': 1, u'model': u'store.book', u'fields': {u'name': u'Mostly Harmless', u'author': [u'Douglas', u'Adams']}}]
>>> newJson = [{
...     "pk": 2,
...     "model": "store.book",
...     "fields": {
...         "name": "Henry",
...         "author": ["Hans"]
...     }
... }]
>>> jsonFromFile.append(newJson[0])
>>> jsonFromFile
[{u'pk': 1, u'model': u'store.book', u'fields': {u'name': u'Mostly Harmless', u'author': [u'Douglas', u'Adams']}}, {'pk': 2, 'model': 'store.book', 'fields': {'name': 'Henry', 'author': ['Hans']}}]
>>> json.dump(jsonFromFile, fileLoop)
>>> fileLoop.close()
于 2014-03-28T12:36:03.463 回答
0

为避免创建数千兆字节的对象,您可以将每个对象存储在单独的行上。它要求您转储每个对象而不使用用于格式化的换行符(json 字符串本身可能\n像往常一样使用(两个字符)):

import json

with open('output.txt', 'a') as file: # open the file in the append mode
    json.dump(obj, file, 
              separators=',:') # the most compact representation by default
    file.write("\n")
于 2014-03-30T20:07:58.123 回答