Python 带嵌套字典的命名占位符 (JSON)

Python named placeholders with nested dictionary (JSON)

我正在处理格式和命名占位符并试图找出: 如何使用 Python 中的命名占位符访问嵌套项(例如 JSON 对象)? 例如

data = {
    'first': 'John', 'last': 'Doe',
    'kids': {
        'first': 'number1',
        'second': 'number2',
        'third': 'number3'
    }
}  

'{first} {last} {kids}'.format(**data) # Python console
"John Doe {'second': 'number2', 'third': 'number3', 'first': 'number1'}"

但是我的"named placeholder format"怎么写才能输出

 "John Doe, number1, number2, number3"

如能提供有关如何从 JSON 对象获取输出的任何线索,我们将不胜感激。

字符串格式化支持索引;你不必引用键:

'{first} {last}, {kids[first]}, {kids[second]}, {kids[third]}'.format(**data)

演示:

>>> '{first} {last}, {kids[first]}, {kids[second]}, {kids[third]}'.format(**data)
'John Doe, number1, number2, number3'