简体   繁体   English

在遍历 for 循环时追加 json 文件

[英]Append json files while iterating through for loop

I want to iterate through some range of pages and save all of them into one json file, that is append page 2 to page 1 and page 3 to already appended page2 to page1.我想遍历一系列页面并将它们全部保存到一个 json 文件中,即将第 2 页附加到第 1 页,将第 3 页附加到已附加到第 1 页的第 2 页。

for i in range(4):
    response = requests.post("https://API&page="+str(i))
    data = response.json()
    my_data = json.load(open( "data.json" ))
    my_data.update(my_data)
    json.dump(data, open( "data.json", 'w' ))

Basing on some answers from similar question I wrote something like that, but it overwrites instead of appending one page to another.基于类似问题的一些答案,我写了类似的东西,但它会覆盖而不是将一页附加到另一页。

The json data structure is as follows: json数据结构如下:

ending with page number that increments every page.以每页递增的页码结尾。

Any idea what I did wrong?知道我做错了什么吗?

Your code is overwriting the contents of the data.json file on each iteration of the loop.您的代码会在循环的每次迭代中覆盖data.json文件的内容。 This is because you are using the 'w' mode when calling json.dump , which will overwrite the contents of the file.这是因为您在调用json.dump时使用了'w'模式,这将覆盖文件的内容。

To append the data to the file, you can use the 'a' mode instead of 'w' when calling json.dump .要将数据附加到文件,您可以在调用json.dump时使用'a'模式而不是'w' This will append the data to the end of the file, rather than overwriting the contents.这会将数据附加到文件末尾,而不是覆盖内容。

Like this像这样

for i in range(4):
    response = requests.post("https://API&page="+str(i))
    data = response.json()
    my_data = json.load(open( "data.json" ))
    my_data.update(my_data)
    json.dump(data, open( "data.json", 'a' ))

What is it that you are trying to achieve?你想要达到什么目的?

You are overwriting the file data.json each time with the result of the response saved in the variable data .您每次都用保存在变量data中的响应结果覆盖文件data.json

Your code has 2 issues: you are updating a dictionary and you are overwriting a file.您的代码有两个问题:您正在更新字典和覆盖文件。 Any of the two could solve your problem, depending on what you want to achieve.两者中的任何一个都可以解决您的问题,具体取决于您想要实现的目标。

It looks like you instead want to save the contents of my_data like that:看起来您反而想像这样保存my_data的内容:

json.dump(my_data, open( "data.json", 'w' ))

Anyway, my_data will be a dictionary that gets its contents overwritten each time.无论如何, my_data将是一个每次都会覆盖其内容的字典。 Depending on the structure of data , this could not be what you want.根据data的结构,这可能不是您想要的。

I'll explain better: if your structure is, for any page, something like我会更好地解释:如果您的结构对于任何页面来说都是

{
    "username": "retne",
    "page": <page-number>
}

my_data will just be equal to the last data page. my_data将等于最后一个data页。

Moreover, about the second issue, if you open the file in 'w' mode, you will always overwrite it.此外,关于第二个问题,如果您以'w'模式打开文件,您将始终覆盖它。 If you will open it in 'a' mode, you will append data to it, obtaining something like this:如果您将以'a'模式打开它,您将向其附加数据,获得如下内容:

{
    "username": "retne",
    "page": 1
}
{
    "username": "pentracchiano",
    "page": 2
}
{
    "username": "foo",
    "page": 3
}

but this is not a valid .json file, because it contains multiple objects with no delimiters.但这不是有效的.json文件,因为它包含多个没有分隔符的对象。

Try being clearer about your intents and I can provide additional support.尝试更清楚地了解您的意图,我可以提供额外的支持。

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

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