Answer the question
In order to leave comments, you need to log in
JSON Keys in the dictionary are duplicated for some reason?
I had this problem several times, each time I found a solution, but then I encounter it again. This time I can't figure out what the problem is anymore.
I will be glad if you tell me.
I make a chat bot, write user data to a file, and read the data from there once during the bot launch. I use json library, python3.
Keys begin to be duplicated after the first write to the file.
This is how I read from a file:
def reloadz():
global users, payments
try:
with open(path + 'data.json', 'r', encoding='utf-8') as f:
datz = json.loads(f.read())
users = datz['users']
payments = datz['payments']
print(datz)
except:
with open(path + 'data.json', 'w') as f:
f.write(json.dumps({'users': {}, 'payments': []}))
users = {}
payments = []
reloadz()
with open(path + 'data.json', 'w') as f:
f.write(json.dumps({'users': users, 'payments': payments}))
{"users": {"603660417": {"sub": false, "current_menu": "main"}, "603660417": {"sub": true, "current_menu": "sub"}}, "payments": [16019644137]}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question