I got this code for implementing my needing:
import json
json_data = []
with open("trendingtopics.json") as json_file:
json_data = json.load(json_file)
for category in json_data:
print category
for trendingtopic in category:
print trendingtopic
And this is my json file:
{
"General": ["EPN","Peña Nieto", "México","PresidenciaMX"],
"Acciones politicas": ["Reforma Fiscal", "Reforma Energética"]
}
However I'm getting this printed:
Acciones politicas
A
c
c
i
o
n
e
s
p
o
l
i
t
i
c
a
s
General
G
e
n
e
r
a
l
I want to get a dictionary being Strings the keys and got a list as value. Then iterate over it. How can I accomplish it?
json_data is a dictionary. In your first loop, you're iterating over a list of the dictionary's keys:
for category in json_data:
category will contain key strings - General and Acciones politicas.
You need to replace this loop, which iterates over keys' letters:
for trendingtopic in category:
with the below, so that it iterates over the dictionary elements:
for trendingtopic in json_data[category]:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With