简体   繁体   English

For 循环中递增 Append 到 JSON 文件

[英]Incrementally Append to JSON File in a For Loop

Is there a way to append single JSON objects to a json file while in a for loop in python. I would prefer not store all my data in one giant json object and dump it all at once, as I am planning on performing millions of API requests.有没有办法在 python 的 for 循环中将 append 单个 JSON 对象放入 json 文件。我不想将所有数据存储在一个巨型 json object 中,并将其全部转储到 json object要求。 I would like to make a single API request, dump the result into a JSON file and then move to the next API request and dump that into the same JSON file.我想发出一个 API 请求,将结果转储到 JSON 文件中,然后转到下一个 API 请求并将其转储到同一个JSON 文件中。

The below code overwrites the JSON file, I am looking for something that appends.下面的代码覆盖了 JSON 文件,我正在寻找附加的东西。

for url in urls:
    r = sesh.get(url)
    data = r.json()

    with open('data.json', 'w') as outfile:
        json.dump(data, outfile)

Such that:这样:

with open('data.json') as outfile:
    data = json.load(data, outfile)

type(data)
>> dict

r.json looks something like this: r.json 看起来像这样:

{'attribute1':1, 'attribute2':10}

Update 更新

Well since I don't have access to your API I just placed some sample responses, in the format you supplied, inside an array. 好吧,因为我无法访问您的API,所以我只是以您提供的格式在数组中放置了一些示例响应。

import json

urls = ['{"attribute1":1, "attribute2":10}', '{"attribute1":67, "attribute2":32}', '{"attribute1":37, "attribute2":12}'];
json_arr = []

for url in urls:
    data = json.loads(url)
    json_arr.append(data)
    with open('data.json', 'w') as outfile:
        json.dump(json_arr, outfile)

Basically we keep an array and append each API response to that array. 基本上我们保留一个数组并将每个API响应附加到该数组。 Then, we can write the accumulative JSON to a file. 然后,我们可以将累积的JSON写入文件。 Also if you want to update the same JSON file on different executions of the code, you can just read the existing output file into an array, in the beginning of the code, and then carry on with my example. 此外,如果您想在代码的不同执行中更新相同的JSON文件,您可以在代码的开头将现有的输出文件读入数组,然后继续我的示例。



Change write mode to append 更改写入模式以追加

Try changing this: 尝试改变这个:

with open('data.json', 'a') as outfile:

To this: 对此:

 with open('data.json', 'a') as outfile: 

The previous answer is surprisingly close to what you need to do.之前的答案出奇地接近您需要做的事情。 So I will build upon it.所以我会以此为基础。

import json

json_arr = ['{"attribute1":1, "attribute2":10}', '{"attribute1":67, "attribute2":32}', '{"attribute1":37, "attribute2":12}'];

with open('data.json', 'w') as outfile:
  outfile.write('[')

for element in json_arr:
    with open('data.json', 'w') as outfile:
        json.dump(element, outfile)
        outfile.write(',')
with open('data.json', 'a') as outfile:
  outfile.write(']')

Was this ^ problem ever solved?这个问题解决了吗? Having a similar issues where after running a loop, my json becomes invalid.有一个类似的问题,在运行循环后,我的 json 变得无效。 I am pulling out all of the objects in a for loop where a key matches a certain name.我在 for 循环中拉出所有对象,其中一个键与某个名称匹配。 if that matches, I append it to a list and then at the end I am calling json.dumps on that list.如果匹配,我将它附加到一个列表中,然后最后我在该列表上调用 json.dumps。 but I am losing the proper json format to keep the files valid.但我丢失了正确的 json 格式以保持文件有效。 Any help would be amazing!任何帮助都会很棒! Example: The top 2 json objects were pulled from one file, and the bottom two were pulled from another one.示例:前 2 个 json 对象从一个文件中拉取,后两个 json 对象从另一个文件中拉取。 but the format is now shot.但格式现在拍摄。

 [ { "item_name": "Dusty Rhodes", "player_name": "Dusty Rhodes", "team": null, "year": null, "images": [ { "url": "", "path": "" } ] }, { "item_name": "Dusty Rhodes", "player_name": "Dusty Rhodes", "team": null, "year": null, "images": [ { "url": "", "path": "" ] } ][ { "item_name": "Dusty Rhodes", "player_name": "Dusty Rhodes", "team": "", "year": null, "images": [] } ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM