Answer the question
In order to leave comments, you need to log in
How can you convert a dictionary key to an int?
Hello, there is a small script for uploading data from a JSON file.
It works great and loads the information I need into the dictionary, but the fact is that in the dictionary the value of the key should be int, but it turns out something like this (the keys marked with an arrow are my problem):
Script for adding data:
with open('data.json', 'w', encoding='utf-8') as fh:
fh.write(json.dumps(data, sort_keys=True, indent=4, ensure_ascii=False))
with open('data.json', 'r', encoding='utf-8') as fh:
data = json.load(fh)
for key, value in data.items():
data[int(key)] = value
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