Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

160
Vistas
Updating A Json in a more efficient way
[
{"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

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

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)
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda