2015-10-16 78 views
0

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

嵌套字典

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']. 
+0

如果你不知道你实际需要访问哪些键,什么决定你的'format()'参数? – TigerhawkT3

+0

我需要{0}中的所有键名和{1}中的所有['名称']值以及{2}中的所有['OS']值。 – iNoob

+0

我确实认为我需要在词典中使用iteritems,但是我无法为我的生活制定出如何做到这一点。格式:$ – iNoob

回答

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

这么简单,哈哈。我有时会问自己。 – iNoob

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 
       ) 
+0

感谢输入乔纳森,我选择了老虎回答,但我已经注意到分裂html头部 – iNoob