Python - JSON从文件加载不起作用

时间:2023-01-24 09:50:50

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.

所以我正在编写一个基本的多用途脚本,它使用json从文件中导入字典但由于某种原因它无法正确保存。我看了一遍,找不到与我确切问题有关的任何事情。

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:

注意:目前该文件只包含在其中:

{}

1 个解决方案

#1


38  

You want json.load for loading a file. json.loads is for loading from a string.

你想要json.load来加载文件。 json.loads用于从字符串加载。

#1


38  

You want json.load for loading a file. json.loads is for loading from a string.

你想要json.load来加载文件。 json.loads用于从字符串加载。