python - 在类中打开转换为字典的 JSON 文件
问题描述
我正在打开并读取一个名为的 json 文件,例如,my_file.json
位于..\config\my_file.json
一个名为run_test.py
. 最终,我想将 JSON 对象转换为 python 字典并在 python 中用它做其他事情。我正在从我的终端运行以下代码来执行该类:
python -c "from run_test import Update; x = Update('my_file.json'); print(x.why())"
这将返回:
Traceback (most recent call last): File "<string>", line 1, in <module> TypeError: why() missing 1 required positional argument: 'file_details'
我不知道为什么我会收到这个错误。当我运行时(return f
在 read_file 的正文中):
python -c "from run_test import Update; x = Update('my_file.json'); print(x.read_file())
这会打印出我需要的字典。但是,我想把它传递给为什么我可以用那本字典做其他事情。然而,当我传递f
到原因时,我得到了上述错误。为什么?
这是我正在使用的课程:
import json
class Update:
def __init__(self, config_file_name):
self.config_file_name = config_file_name
def why(self, file_details):
return file_details
def read_file(self):
f = json.load(open('..\config\\' + self.config_file_name))
self.why(f)
解决方案
推荐阅读
- shell - shell 脚本中的 Group_by 和 group_concat
- java - Java - 返回不同的方法
- data-visualization - 使用自定义 3D 模型进行地理空间数据可视化的工具
- docker-compose - Dockerized certbot 找不到挂载的目录
- kubernetes - 使用 CURL 获取系统的 Kubernetes API 测试:匿名问题
- image - 在颤振中使用 POST API 调用将图像传递给 REST
- ssl - nginx add ssl 接口并发测试耗时比http长10倍
- bootstrap-table - 如何在下拉列表更改时更改表的数据源
- python - Python:定期在线程中运行http请求
- python - youtube数据api,外语支持