So I have a data in json file which i never worked before.When I load and try print the data by index like print(data[0])
I got and warning
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/app/extra/plugins/python/helpers/pydev/_pydev_bundle/pydev_umd.py", line 198, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "/app/extra/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "/home/levi/PycharmProjects/Topic-Modelling/main.py", line 37, in <module>
print(data[0])
KeyError: 0
But When I print it like a print(data["0"])
the result as fine as expected.So I am confused the dict indexes change to string not integer.Can anyone tell me how to resolve this problem by change the keys into int.My code is like this
import json
def load_data(file):
with open(file, "r", encoding="utf-8") as f:
data = json.load(f)
return data
def write_data(file, data):
with open(file, "w", encoding="utf-8") as f:
json.dump(data, f, indent=4)
data = load_data("file.json")["teks"]
print(data["0"])