使用嵌套字典进行字符串格式化

时间:2015-10-16 02:40:27

标签: python dictionary formatting

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

嵌套词典

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>
'''

写入档案

with open("Summary.html", "w+") as outfile:
    outfile.write(HEADER.format(device_dic[0], device_dic['Name'], device_dic['OS'])) 
#Potentially multiple items of each as shown in test dictionary. The `Devicename` varies so cant be called by string example ['OS'].

2 个答案:

答案 0 :(得分:1)

循环访问字典以访问其内容。您可以使用items()字典方法一起遍历其键和值:

>>> 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>

答案 1 :(得分:1)

以下代码使用列表迭代[ 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
                  )