2015-10-16 82 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