Python 使用嵌套字典设置字符串格式

Python 使用嵌套字典设置字符串格式,python,dictionary,formatting,Python,Dictionary,Formatting,我试图通过字符串格式从嵌套字典中写入多个键和相关值。我尝试过各种方法,但由于它是嵌套的,我似乎没有太多的运气。这可能吗 嵌套字典 defaultdict(None, {'Devicename': {'OS': 'version', 'Name': 'name'}, 'Devicename': {'OS': 'version', 'Name': 'name'}}) 格式化数据 HEADER = ''' <html> <head> <h2>

我试图通过字符串格式从嵌套字典中写入多个键和相关值。我尝试过各种方法,但由于它是嵌套的,我似乎没有太多的运气。这可能吗

嵌套字典

defaultdict(None, {'Devicename': {'OS': 'version', 'Name': 'name'}, 'Devicename': {'OS': 'version', 'Name': 'name'}})
格式化数据

HEADER = '''
<html>
    <head>
        <h2>Summary</h2>
        <tr>
        <td><b>Device:</b> {0}</td>
        </tr>
        <table style="width:80%">
        <tr>
         <td><b>Name:</b> {1}</td>
         <td><b>OS:</b> {2}</td>        
        </tr>
        </table>
    </head>
    <body>
'''

循环浏览字典以访问其内容。您可以使用
items()
dictionary方法将其键和值循环到一起:

>>> a = collections.defaultdict(None, {'Devicename1': {'OS': 'version', 'Name': 'name'}, 'Devicename2': {'OS': 'version', 'Name': 'name'}})
>>> HEADER = '''
... <html>
...     <head>
...         <h2>Summary</h2>
...         <tr>
...         <td><b>Device:</b> {0}</td>
...         </tr>
...         <table style="width:80%">
...         <tr>
...          <td><b>Name:</b> {1}</td>
...          <td><b>OS:</b> {2}</td>
...         </tr>
...         </table>
...     </head>
...     <body>
... '''
>>> for key,d in a.items():
...     print(HEADER.format(key, d['Name'], d['OS']))
...

<html>
    <head>
        <h2>Summary</h2>
        <tr>
        <td><b>Device:</b> Devicename2</td>
        </tr>
        <table style="width:80%">
        <tr>
         <td><b>Name:</b> name</td>
         <td><b>OS:</b> version</td>
        </tr>
        </table>
    </head>
    <body>


<html>
    <head>
        <h2>Summary</h2>
        <tr>
        <td><b>Device:</b> Devicename1</td>
        </tr>
        <table style="width:80%">
        <tr>
         <td><b>Name:</b> name</td>
         <td><b>OS:</b> version</td>
        </tr>
        </table>
    </head>
    <body>
>a=collections.defaultdict(无,{'Devicename1':{'OS':'version','Name':'Name'},'Devicename2':{'OS':'version','Name':'Name'})
>>>标题=“”
... 
...     
...         总结
...         
...         设备:{0}
...         
...         
...         
...          名称:{1}
...          OS:{2}
...         
...         
...     
...     
... '''
>>>对于a.items()中的键d:
...     打印(HEADER.format(key,d['Name'],d['OS']))
...
总结
设备:Devicename2
姓名:姓名
操作系统:版本
总结
设备:Devicename1
姓名:姓名
操作系统:版本

循环浏览字典以访问其内容。您可以使用
items()
dictionary方法将其键和值循环到一起:

>>> a = collections.defaultdict(None, {'Devicename1': {'OS': 'version', 'Name': 'name'}, 'Devicename2': {'OS': 'version', 'Name': 'name'}})
>>> HEADER = '''
... <html>
...     <head>
...         <h2>Summary</h2>
...         <tr>
...         <td><b>Device:</b> {0}</td>
...         </tr>
...         <table style="width:80%">
...         <tr>
...          <td><b>Name:</b> {1}</td>
...          <td><b>OS:</b> {2}</td>
...         </tr>
...         </table>
...     </head>
...     <body>
... '''
>>> for key,d in a.items():
...     print(HEADER.format(key, d['Name'], d['OS']))
...

<html>
    <head>
        <h2>Summary</h2>
        <tr>
        <td><b>Device:</b> Devicename2</td>
        </tr>
        <table style="width:80%">
        <tr>
         <td><b>Name:</b> name</td>
         <td><b>OS:</b> version</td>
        </tr>
        </table>
    </head>
    <body>


<html>
    <head>
        <h2>Summary</h2>
        <tr>
        <td><b>Device:</b> Devicename1</td>
        </tr>
        <table style="width:80%">
        <tr>
         <td><b>Name:</b> name</td>
         <td><b>OS:</b> version</td>
        </tr>
        </table>
    </head>
    <body>
>a=collections.defaultdict(无,{'Devicename1':{'OS':'version','Name':'Name'},'Devicename2':{'OS':'version','Name':'Name'})
>>>标题=“”
... 
...     
...         总结
...         
...         设备:{0}
...         
...         
...         
...          名称:{1}
...          OS:{2}
...         
...         
...     
...     
... '''
>>>对于a.items()中的键d:
...     打印(HEADER.format(key,d['Name'],d['OS']))
...
总结
设备:Devicename2
姓名:姓名
操作系统:版本
总结
设备:Devicename1
姓名:姓名
操作系统:版本

以下代码使用列表迭代
[expr(arg)for arg in list]
对设备进行枚举,并使用字典解包来提供格式参数

from collections import defaultdict

device_dic = defaultdict(None, {'Devicename1': {'OS': 'version1', 'Name': 'name1'}, 'Devicename2': {'OS': 'version2', 'Name': 'name2'}})

HEADER1 = '''
<html>
    <head>
'''

# Split this since we'll have multiple copies
# Note that the {} use named arguments for format here. {OS}, etc.
HEADER2 = '''
        <h2>Summary</h2>
        <tr>
        <td><b>Device:</b> {0}</td>
        </tr>
        <table style="width:80%">
        <tr>
         <td><b>Name:</b> {OS}</td>
         <td><b>OS:</b> {Name}</td>        
        </tr>
        </table>
'''
HEADER3 = '''
    </head>
    <body>
'''

with open("Summary.html", "w+") as outfile:
    outfile.write(HEADER1
                  # We iterate over the items in the dictionary here.
                  # The **value unpacks the nested dictionary. see https://docs.python.org/2/tutorial/controlflow.html#unpacking-argument-lists
                  + "".join([HEADER2.format(key, **value) for key, value in device_dic.items()]) \
                  + HEADER3
                  )
从集合导入defaultdict
device_dic=defaultdict(无,{'Devicename1':{'OS':'version1','Name':'name1'},'Devicename2':{'OS':'version2','Name':'name2'})
标题1=''
'''
#拆分此文件,因为我们将有多个副本
#注意,{}在这里使用命名参数作为格式。{OS}等。
标题2=''
总结
设备:{0}
名称:{OS}
OS:{Name}
'''
标题3=''
'''
以open(“Summary.html”、“w+”)作为输出文件:
输出文件写入(标题1
#我们在这里迭代字典中的项目。
#**值解压嵌套字典。请参阅https://docs.python.org/2/tutorial/controlflow.html#unpacking-参数列表
+“”.join([HEADER2.format(key,**value)表示键,值在设备目录项()中)\
+校长3
)

以下代码使用列表迭代
[expr(arg)for arg in list]
对设备进行枚举,并使用字典解包来提供格式参数

from collections import defaultdict

device_dic = defaultdict(None, {'Devicename1': {'OS': 'version1', 'Name': 'name1'}, 'Devicename2': {'OS': 'version2', 'Name': 'name2'}})

HEADER1 = '''
<html>
    <head>
'''

# Split this since we'll have multiple copies
# Note that the {} use named arguments for format here. {OS}, etc.
HEADER2 = '''
        <h2>Summary</h2>
        <tr>
        <td><b>Device:</b> {0}</td>
        </tr>
        <table style="width:80%">
        <tr>
         <td><b>Name:</b> {OS}</td>
         <td><b>OS:</b> {Name}</td>        
        </tr>
        </table>
'''
HEADER3 = '''
    </head>
    <body>
'''

with open("Summary.html", "w+") as outfile:
    outfile.write(HEADER1
                  # We iterate over the items in the dictionary here.
                  # The **value unpacks the nested dictionary. see https://docs.python.org/2/tutorial/controlflow.html#unpacking-argument-lists
                  + "".join([HEADER2.format(key, **value) for key, value in device_dic.items()]) \
                  + HEADER3
                  )
从集合导入defaultdict
device_dic=defaultdict(无,{'Devicename1':{'OS':'version1','Name':'name1'},'Devicename2':{'OS':'version2','Name':'name2'})
标题1=''
'''
#拆分此文件,因为我们将有多个副本
#注意,{}在这里使用命名参数作为格式。{OS}等。
标题2=''
总结
设备:{0}
名称:{OS}
OS:{Name}
'''
标题3=''
'''
以open(“Summary.html”、“w+”)作为输出文件:
输出文件写入(标题1
#我们在这里迭代字典中的项目。
#**值解压嵌套字典。请参阅https://docs.python.org/2/tutorial/controlflow.html#unpacking-参数列表
+“”.join([HEADER2.format(key,**value)表示键,值在设备目录项()中)\
+校长3
)

如果您不知道实际需要访问哪些键,是什么决定了您的
format()
参数?我需要{0}中的所有键名,{1}中的所有['Name']值和{2}中的所有['OS']值.我确实认为我需要在dict上写下这些,但我一辈子都无法解决如何在dict中完成这些。格式:$您是否在循环中写入文件,例如设备中的d的
。\u dic:
。。。和
.format(d,d['Name',d['OS'])
?写入文件不是循环的否,但是如果没有其他方法,我想我必须走这条路,如果可能的话。如果你不知道你实际上需要访问哪些键,那么是什么决定了你的
format()
参数?我需要{0}中的所有键名和{1}中的所有['Name']值以及{2}中的所有['OS']值。我确实认为我需要在dict上写下这些值,但我一辈子都不知道如何在dict中做到这一点。format:$您是否在循环中写入文件,例如设备中d的
。。。和
.format(d,d['Name',d['OS'])
?写入文件不是循环的否,但如果没有其他方法,我想我必须走这条路,如果可能的话谢谢输入Jonathan,我选择了Tigers的答案,但我注意到了拆分html
sthanks的输入Jonathan,我选择了Tigers的答案,但我注意到了拆分html
标题的方法