Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
unpack dict
#1
Giving the following example:
How can one unpack the dict

Current output:
Output:
Class: Wizard NAME Ralph AGE 20 SPELLS {'fireball': 10, 'firestorm': 15} NAME George AGE 20 SPELLS {'firestorm': 15, 'meteor': 30}
Target output

Output:
Class: Wizard NAME Ralph AGE 20 SPELLS: fireball: 10, firestorm: 15 NAME George AGE 20 SPELLS: firestorm: 15, meteor: 30
character.json
{
    "wizard": [
        {
            "name": "Ralph",
            "age": 20,
            "spells": {"fireball": 10, "firestorm": 15}
        },

        {
            "name": "George",
            "age": 20,
            "spells": {"firestorm": 15, "meteor": 30}
        }
    ]
}
import json
file = "character.json"

with open(file, 'r') as rfile:
    character = json.load(rfile)

for character_class, data in character.items():
    print(f'Class: {character_class.title()}')
    for stats in data:
        for key, value in stats.items():
            print(key.upper(), value)
        print()
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts


Reply
#2
I got this to work
import json
file = "character.json"

with open(file, 'r') as rfile:
    character = json.load(rfile)

for character_class, data in character.items():
    print(f'Class: {character_class.title()}')
    for stats in data:
        temp_list = []
        for key, value in stats.items():
            if isinstance(value, dict):
                for k, v in value.items():
                    temp_list.append(f'{k}: {v}')
                string = ', '.join(temp_list)
            else:
                string = value
            print(f'{key.upper()}: {string}')
        print()
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts


Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  DEC pack, unpack and disk-images Curbie 32 7,273 Aug-23-2024, 03:37 PM
Last Post: Curbie
  Too much values to unpack actualpy 3 1,505 Feb-11-2024, 05:38 PM
Last Post: deanhystad
  ValueError: not enough values to unpack (expected 4, got 1) vlearner 2 8,444 Jan-28-2022, 06:36 PM
Last Post: deanhystad
  [SOLVED] [geopy] "ValueError: too many values to unpack (expected 2)" Winfried 2 3,776 Mar-30-2021, 07:01 PM
Last Post: Winfried
  Cannot unpack non-iterable NoneType object, i would like to ask for help on this. Jadiac 3 11,473 Oct-18-2020, 02:11 PM
Last Post: Jadiac
  subprogram issues: cannot unpack non-iterable function object error djwilson0495 13 8,906 Aug-20-2020, 05:53 PM
Last Post: deanhystad
  struct.unpack failed Roro 2 4,567 Jun-13-2020, 05:28 PM
Last Post: DreamingInsanity
  Sort a dict in dict cherry_cherry 4 104,451 Apr-08-2020, 12:25 PM
Last Post: perfringo
  Can't unpack values of dictionary with ** Snake 3 5,016 Mar-11-2020, 11:17 AM
Last Post: Snake
  Error: too many values to unpack Mike 1 6,142 Oct-30-2019, 03:07 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020