简体   繁体   English

读取json文件python

[英]reading json file python

js = {"Alex":{"b_":{"ep_0":"[1,2,3]"}, "g_":{"ep_0":"[3,4,5]"}, "f_":{"ep_0":"[3,4,5]", "ep_1":"[3,4,5]"}},
      "Sam":{"b_":{"ep_0":"[1,2,3]"}, "g_":{"ep_0":"[3,4,5]"}},
      "Joe":{"b_":{"ep_0":"[1,2,3]"}, "g_":{"ep_0":"[3,4,5]"}, "f_":{"ep_1":"[31,44,56]"}}
      }

I need to read ep_0 and ep_1 for each user, here is my snipped code: 我需要为每个用户阅读ep_0和ep_1,这是我的摘录代码:

users = [i for i in js.keys()]
data = {}
final_data = {}
for key in users:
        for user in js[key].keys():
            if 'f_' not in key:
                continue
            for z in js[users]['f_']:
                if 'ep_0' not in z:
                    continue
                data['ep0'] = js[user]['f_']['ep_0']
                if 'ep_1' not in z:
                    continue
                data['ep1'] = js[user]['f_']['ep_1']
                final_data[user] = data

    print(final_data)

the output of my code is {} and desire output should be: 我的代码的输出为{},期望的输出应为:

{'Alex': {'f_':{'ep_0':'[3,4,5]', 'ep_1':'[3,4,5]'}}, 'Joe': { 'f_':{'ep_1':'[31,44,56]'}} }

Here is my simple solution to your problem. 这是我为您解决的简单方法。

final_data = {}
for user in js.keys():
    if 'f_' not in js[user]:
        continue
    final_data[user] = { "f_": js[user]['f_']}

print(final_data)

Output 产量

{'Alex': {'f_': {'ep_0': '[3,4,5]', 'ep_1': '[3,4,5]'}}, 'Joe': {'f_': {'ep_1': '[31,44,56]'}}}

I think that your most glaring problem is 我认为您最明显的问题是

users = [i for i in js.keys()]
...
        for z in js[users]['f_']:

users is a poorly-formed list of the keys in your dict. users是字典中格式不正确的键列表。 Just what do you expect js[users] to mean? 您对js[users]期望是什么?

However, your first problem is the combination 但是,您的第一个问题是组合

for key in users:
    for user in js[key].keys():
        if 'f_' not in key:

users is the list of names: Alex, Sam, Joe. users是姓名列表:Alex,Sam,Joe。 How do you expect to find f_ in that list? 您希望如何在该列表中找到f_ You've incorrectly connected your variables. 您错误地连接了变量。


I strongly recommend that you start over with your coding. 我强烈建议您从编码开始。 Employ incremental programming . 采用增量编程 Write a couple of lines that do one step of your process. 编写几行代码,即可完成过程的一个步骤。 Insert print statements to test that they do what you expect. 插入print语句以测试它们是否达到了您的期望。 Only after you've proved that, do you add more lines. 只有证明了这一点之后,您才能添加更多行。

The problem you're facing here is that you wrote too much code at once, making several mistakes, and you're now in a situation where a single fix cannot get you any reasonable output. 您在这里面临的问题是您一次编写了太多代码,犯了一些错误,并且现在您处于一种无法获得任何合理输出的情况。

Been there, done that, and I have all too many t-shirts from my forays into my own creations. 去过那里,做到了,从我的尝试到我自己的创作,我都有太多的T恤。

I'm seeing js[users] in a few places in your code. 我在代码中的几个地方看到了js[users] You probably mean js[user] , since users is just the list of keys from js , not a key itself. 您可能是js[user] ,因为users只是js的键列表,而不是键本身。 Try fixing that and see if it helps you. 尝试修复该问题,看看是否有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM