How to get a dictionary from a json file in Python? -
i got code implementing needing:
import json json_data = [] open("trendingtopics.json") json_file: json_data = json.load(json_file) category in json_data: print category trendingtopic in category: print trendingtopic
and json file:
{ "general": ["epn","peña nieto", "méxico","presidenciamx"], "acciones politicas": ["reforma fiscal", "reforma energética"] }
however i'm getting printed:
acciones politicas c c o n e s p o l t c s general g e n e r l
i want dictionary being strings keys , got list value. iterate on it. how can accomplish it?
json_data dictionary. in first loop, you're iterating on list of dictionary's keys:
for category in json_data:
category contain key strings - general , acciones politicas.
you need replace loop, iterates on keys' letters:
for trendingtopic in category:
with below, iterates on dictionary elements:
for trendingtopic in json_data[category]:
Comments
Post a Comment