When I add a identifier to a dictionary in my python project it works properly however when I try saving my dictionary with json it does not save as I can see in the .txt file.
My code:
import json
def read_file(file):
f = open(file, "r")
file_contents = f.read()
f.close()
return(file_contents)
def write_file(file, content):
f = open(file, "w")
f.write(json.dumps(content))
f.close()
data = {
}
data["user1"] = "100"
write_file("data.txt", data)
file_contents = read_file("data.txt")
print(file_contents)
data["user1"] = "150"
write_file("data.txt", data)
file_contents = read_file("data.txt")
print(file_contents)
I tried changing the code a bit however I would get different errors including an error saying you cant add item identifiers to a str.