I have a dictionary in python. I want to modify that dictionary and then save the dictionary to an external file so that when I load up the python program again it grabs the dictionary data from the external file.
class Data:
"""
Data handling class to save
and receive json data, parent
of User for data purposes.
"""
def saveData(data, file):
with open(file, 'r+') as dataFile:
dataFile.write(json.dumps(data))
def getData(file):
with open(file, 'r+') as dataFile:
return json.loads(dataFile.readline())
def deleteContent(file):
file.seek(0)
file.truncate()
But when I write to the file and then try to read it reads it as a string and I can't use the read data to set a dictionary. How can I get data in a dictionary from an external JSON file as dictionary data, not string data?
data = Data.getData("chatbotData.json")
dataDict = data
dataDict["age"] = 2
Here is what I want to do with the data and I get this error:
TypeError: 'str' object does not support item assignment