Python - 从文件中加载 JSON 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26072148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python - JSON Load from file not working
提问by Arctoran
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:
注意:目前该文件只有内部:
{}
采纳答案by BrenBarn
You want json.loadfor loading a file. json.loadsis for loading from a string.
你想要json.load加载一个文件。 json.loads用于从字符串加载。

