Reputation: 279
I have a json file with a dictionary, and lists inside that dictionary
{"Dogs": [["spot"], 1], "Cats": [["whiskers"], 1], "fish": [["bubbles", "lefty", "tank", "goldie"], 4], "elephant": [["tiny", "spring"], 2], "zebra": [[], 1], "gazelle": [["red", "blue", "green", "yellow", "gold", "silver"], 6]}
There's more in the dictionary but from this example you can see the format.
with open('myfile.json', 'r') as myfile:
json_data = json.load(myfile)
for e,([v], z) in json_data.items():
print e, v, z
This gives me a
too many values to unpack error.
I want the output to look like
dogs spot 1
cats whiskers 1
Upvotes: 0
Views: 75
Reputation: 706
much simpler since the value is always going to be a list:
with open('myfile.json', 'r') as myfile:
json_data = json.load(t)
for e, v in json_data.items():
print e, " ".join(v[0]), v[1]
This gives:
gazelle red blue green yellow gold silver 6
fish bubbles lefty tank goldie 4
Cats whiskers 1
zebra 1
elephant tiny spring 2
Dogs spot 1
Upvotes: 0
Reputation: 21609
Presuming you want all animal names (?) output
...
for e, (v, z) in json_data.items():
print('%s %s %d' % (e.lower(), ' '.join(v), z))
outputs
gazelle red blue green yellow gold silver 6
fish bubbles lefty tank goldie 4
cats whiskers 1
zebra 1
elephant tiny spring 2
dogs spot 1
Upvotes: 1