Apr-10-2022, 09:48 PM
Giving the example below I know I can use a for loop to reach the name but, was wondering if there is a way to reach it in a one liner? Obviously the
jsonfile:
if name.lower() in data[_class]
does not work. I do not know how to iterate over the dicts in the data[_class] part. Any help would be great.jsonfile:
{ "wizard": [ { "name": "tabol", "age": 23, "skills": { "fireball": 20, "meteor": 15 } }, { "name": "zac", "age": 25, "skills": { "fireball": 10, "meteor": 5 } } ], "cleric": [], "warrior": [] }python snippet
def create_character(file, _class, name, age, skills={}): character = { 'name': name, 'age': age, 'skills': skills } with open(file, 'r+') as json_file: data = json.load(json_file) print(data[_class]) if name.lower() in data[_class]: print('Alreay here') create_character(file, 'wizard', 'zac', 25, {'fireball':10, 'meteor': 5})
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
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts