0
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:

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]'}} }
asked Jul 16, 2019 at 16:23
8
  • What have you tried so far? Commented Jul 16, 2019 at 16:25
  • can you show the expected output? Commented Jul 16, 2019 at 16:26
  • 1
    use print() in different places to see values in variables - this helps you to see where is the problem. Commented Jul 16, 2019 at 16:27
  • Learn to use .items(). Commented Jul 16, 2019 at 16:35
  • 1
    Nothing you're showing here is JSON. Your js = ... is assigning a Python data structure; to be JSON, it would need to have double quotes -- unless you're referring to the lists in the inner strings being JSON? Commented Jul 16, 2019 at 16:36

3 Answers 3

1

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]'}}}
answered Jul 16, 2019 at 17:21
Sign up to request clarification or add additional context in comments.

Comments

0

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. Just what do you expect js[users] to mean?

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. How do you expect to find f_ in that list? 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. 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.

answered Jul 16, 2019 at 16:48

Comments

0

I'm seeing js[users] in a few places in your code. You probably mean js[user], since users is just the list of keys from js, not a key itself. Try fixing that and see if it helps you.

answered Jul 16, 2019 at 16:48

1 Comment

Thank you for mentioning it (it was correct in my code and I edited my question as well). I can get the ep_0 if I remove the condition of ep_1.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.