python - 如何遍历多个 json 字典来检查相同类型键的值?
问题描述
我希望能够在多个 json 字典中获取“delta”的值。如果有任何改变,我正在使用来自 kivy 的 JsonStore。当我按下启动 check_streak 函数的按钮时,我得到 NameError: name 'delta' is not defined。这是代码:
class MainApp(App):
def build(self): # build() returns an instance
self.store = JsonStore("streak.json") # file that stores the streaks:
return presentation
def check_streak(self, instance):
for item in self.store.find(delta):
if delta > time.time():
print("early")
if delta == time.time():
print("on time")
if delta < time.time():
print("late")
这是单独文件中的 json 字典:
{"first": {"action": "first", "action_num": "1", "seconds": 60, "score": 0, "delta": 1555714261.0438898}, "second": {"action": "second", "action_num": "2", "seconds": 120, "score": 0, "delta": 1555879741.894656}}
我希望能够从文件中的每个对象中获取“delta”的值。我该怎么做?
解决方案
那么变量delta
不存在,因此错误。
大概你的意思是for item in self.store.find("delta"):
(以 delta 作为字符串)?
推荐阅读
- python - Visual Studio 2017 或 2019 中的 Django 单元测试:
- java - 如何围绕椭圆移动对象(椭圆形)
- c# - SCNText 未在 SceneKit 中显示
- javascript - 单击带有旋转图像的图像时打开列表
- html - 将联系人表单中的一个标签与顶行而不是中间对齐
- vmware - 将虚拟机搜索限制到特定集群?
- python - TensorFlow/Keras:输出层的形状错误
- javascript - 如何使用 Redux 从一系列复选框中获取值以与其他一些组件共享?
- python - How to repeat the first letter twice using a str?
- c++ - 如何解决这个“'.'之前的预期主表达式'。JarOfJam 部分的令牌?