So I am writing a basic multipurpose script which uses json to import a dictionary from a file but for some reason it doesn't save properly. I've looked all over and can't find anything relating to my exact problem.
Here is my code:
import json
dicti = json.loads(open('database.db'))
print(str(dicti))
But then I get this error:
TypeError: JSON object must be str, not TextIOWrapper.
So does anyone have any ideas on what the problem is? Thanks in Advance.
Note: Currently the file only has inside it:
{}
Load the json file into a file object and read its contents with the file. read() function, which returns a string containing the file's contents. Use the json. loads() function to convert this string object into the required python dictionary and store the result in a variable jsonData.
You want json.load
for loading a file. json.loads
is for loading from a string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With