简体   繁体   中英

Trying to append an item to a Json File but it keeps overwriting it

Below is the code followed by the desired output however, rather than appending to "[game]" it just overwrites it. Another issue is that if the length of the new "user" is smaller than the user in the list it messes up the file. I've played about with it and only the last if statement seems to be causing problems. When I delete the other if statements and run the code it works fine. Any help is appreciated.

import json
def streamon(user, streamer, game=None):
    with open("Test.json","r+") as z: 
        Json_item = json.load(z)
        if streamer not in Json_item:
            new_add = {streamer:[{'Online':0, f'{game}':f'user'}]}
            Json_item.update(new_add)
            z.seek(0)
            json.dump(Json_item, z, indent=4)
    
        if game not in Json_item[streamer]:
            new_add = {game:[]}
            Json_item[streamer][0].update(new_add)
            z.seek(0)
            json.dump(Json_item, z, indent=4)

        if user not in Json_item[streamer][0][game]:
            
            Json_item[streamer][0][game].append(user)
            z.seek(0)
            json.dump(Json_item, z, indent=4)
{
    "https://www.twitch.tv/Syndicate": [
        {
            "Online": 0,
            "https://www.twitch.tv/directory/game/Minecraft": [
                "bob"
            ]
        }
    ],
    "https://www.twitch.tv/Swagg": [
        {
            "Online": 0,
            "https://www.twitch.tv/directory/game/Minecraft": [
                "John"
            ],
            "https://www.twitch.tv/directory/game/Fifa": [
                "I",
                "want',
                "new users", 
                "to append here",
                "rather than overwrite the first value"
            ]
        }
    ]
}

You're going to a lot of extra trouble there, and you're confused about which things are lists and which are dicts. This produces the result you want. Note that you shouldn't rewrite the JSON file after every change. Wait until all the changes are done. And don't use dict.update to set a single value.

Note: the reason yours failed is that Json_item[streamer] is a list of dicts. Thus, if game not in Json_item[streamer] would never be true.

import json
def streamon(user, streamer, game=None):
    with open("test.json","r+") as z: 
        Json_item = json.load(z)
        if streamer not in Json_item:
            Json_item[streamer] = [{'Online':0}]
    
        if game not in Json_item[streamer][0]:
            Json_item[streamer][0][game] = []

        if user not in Json_item[streamer][0][game]:
            Json_item[streamer][0][game].append(user)

        z.seek(0)
        json.dump(Json_item, z, indent=4)

open('test.json','w').write('{}')

streamon( "bob", 
    "https://www.twitch.tv/Syndicate",
    "https://www.twitch.tv/directory/game/Minecraft"
)
streamon( "John", 
    "https://www.twitch.tv/Swagg",
    "https://www.twitch.tv/directory/game/Minecraft"
)
for s in (
            "I",
            "want",
            "new users", 
            "to append here",
            "rather than overwrite the first value"
        ):
    streamon( s,
        "https://www.twitch.tv/Swagg",
        "https://www.twitch.tv/directory/game/Fifa"
    )

Output:

{
    "https://www.twitch.tv/Syndicate": [
        {
            "Online": 0,
            "https://www.twitch.tv/directory/game/Minecraft": [
                "bob"
            ]
        }
    ],
    "https://www.twitch.tv/Swagg": [
        {
            "Online": 0,
            "https://www.twitch.tv/directory/game/Minecraft": [
                "John"
            ],
            "https://www.twitch.tv/directory/game/Fifa": [
                "I",
                "want",
                "new users",
                "to append here",
                "rather than overwrite the first value"
            ]
        }
    ]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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