Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating A Json in a more efficient way

Tags:

python

json

[
{"923390702359048212": 5},
{"462291477964259329": 1},
{"803390252265242634": 3},
{"824114065445486592": 2},
{"832041337968263178": 4}
]

This is a list of user ids that I just randomly made and some sample number that each id has. In this case lets call it a number of goals scored in a season in a video game. As I try to update the amount of goals scored at the end of the game, I have to go in a roundabout way which first gets all the member ids in the server, and then compares it to the data with the following code.

amount = 0
for member in server:
    if member.id != server.member.id:
        amount = amount + 1
    else:
        print(amount)
        print(str(member.id), str(server.member.id))
        print(jsondata[amount][str(server.member.id)])
        break
    
jsondata[amount][str(server.member.id) = jsondata[amount][str(server.member.id)] + 1

Is there a better way to do what I am working on? I know I am going to run into problems eventually as I don't list members on the json until they score a goal and also I feel like I am wasting a ton of time with my program by having it check a lot of members (I edited out my real list and made this one to make it easier as nobody needs to see 50+ entries to get the idea). I am still a beginner when it comes to this so please respond in simple terms or leave links to places I can learn more complicated ideas. Thanks

    def goalscored():
        amount = 0
        for member in server:
            if member.id != server.member.id:
                amount = amount + 1
            else:
                print(amount)
                break
        with open('Goals.json','w') as f:
            jsondata = json.load(f)
            jsondata[amount][str(server.member.id) = jsondata[amount][str(server.member.id)] + 1
            json.dump(jsondata,f)
    
    def firstgoal(server.member.id):
        with open('Goals.json','w') as f:
            jsondata = json.load(f)
            amount = 0
            goalscored = 1
            totalmembers = server.members.amount
            for member in server:
                if membed.id !=server.member.id:
                    amount = amount + 1
                if amount == totalmembers:
                    NewScore = {user.id:goalscored}
                    json.dump(NewScore,f)

Code that I'm using

like image 642
Happyface456 Avatar asked Nov 05 '22 23:11

Happyface456


1 Answers

Not sure why you couldn't get it work earlier, but storing it as dict would be much, much easier.

# file.json
{
 "923390702359048212": 5,
 "462291477964259329": 1,
 "803390252265242634": 3,
 "824114065445486592": 2,
 "832041337968263178": 4
}

def goalscored():
    with open("Goals.json", "r") as f:
        jsondata = json.load(f)
        # Make sure your id is not int but str
        if server.member.id not in jsondata:  # first goal case
            jsondata[server.member.id] = 0
        jsondata[server.member.id] += 1

    with open("Goals.json", "w") as f:
        json.dump(jsondata, f)
like image 168
kosciej16 Avatar answered Nov 11 '22 05:11

kosciej16