Python - JSON从文件加载不起作用 [英] Python - JSON Load from file not working

查看:418
本文介绍了Python - JSON从文件加载不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在编写一个基本的多用途脚本,它使用json从一个文件导入一个字典,但由于某种原因它不能正常保存。我已经查看全部,找不到与我的确切问题有关的任何内容。



这是我的代码:




print(str(dicti))

code>

但是我收到这个错误:

  TypeError:JSON对象必须是str,而不是TextIOWrapper。 

有没有人对什么问题有任何想法?感谢提前。



注意:目前该文件只有内部:

  {} 


解决方案

c> json.load 用于加载文件。 json.loads 用于从字符串加载。


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:

{}

解决方案

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

这篇关于Python - JSON从文件加载不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆