用嵌套字典格式化字符串 [英] String formating with nested dictionary

查看:190
本文介绍了用嵌套字典格式化字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过字符串格式来从嵌套字典中编写多个键和相关的值。我已经尝试了各种方法,但因为它嵌套,我似乎没有太多的运气。这可能吗?



嵌套字典

  defaultdict(None,{'Devicename':{'OS':'version','Name':'name'},'Devicename':{'OS':'version','Name':'name'} })

格式化数据

  HEADER ='''
< html>
< head>
< h2>总结< / h2>
< tr>
< td>< b>设备:< / b> {0}< / TD>
< / tr>
< table style =width:80%>
< tr>
< td>< b>名称:< / b> {1}< / TD>
< td>< b>操作系统:< / b> {2}< / TD>
< / tr>
< / table>
< / head>
< body>
'''

写入文件



$ $ p $ 用open(Summary.html,w +)作为outfile:
outfile.write(HEADER.format(device_dic [ 0],device_dic ['Name'],device_dic ['OS']))
#每个测试字典中可能包含多个项目。 Devicename变化了,所以不能被字符串例子['OS']调用。


解决方案

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

 >>> a = collections.defaultdict(None,{'Devicename1':{'OS':'version','Name':'name'},'Devicename2':{'OS':'version','Name':'name '}})
>>> HEADER ='''
...< html>
...< head>
...< h2>汇总< / h2>
...< tr>
...< td>< b>设备:< / b> {0}< / TD>
...< / tr>
...< table style =width:80%>
...< tr>
...< td>< b>名称:< / b> {1}< / TD>
...< td>< b>操作系统:< / b> {2}< / TD>
...< / tr>
...< / table>
...< / head>
...< body>
...'''
>>>对于key,d在a.items()中:
... print(HEADER.format(key,d ['Name'],d ['OS']))
...

< html>
< head>
< h2>总结< / h2>
< tr>
< td>< b>设备:< / b> Devicename2< / TD>
< / tr>
< table style =width:80%>
< tr>
< td>< b>名称:< / b>命名< / TD>
< td>< b>操作系统:< / b>版本< / TD>
< / tr>
< / table>
< / head>
< body>


< html>
< head>
< h2>总结< / h2>
< tr>
< td>< b>设备:< / b> Devicename1< / TD>
< / tr>
< table style =width:80%>
< tr>
< td>< b>名称:< / b>命名< / TD>
< td>< b>操作系统:< / b>版本< / TD>
< / tr>
< / table>
< / head>
< body>


I'm trying to write multiple keys and associated values from a nested dictionary via string formatting. I have tried various approaches but as it is nested I don't seem to be having much luck. Is this possible?

Nested Dictionary

defaultdict(None, {'Devicename': {'OS': 'version', 'Name': 'name'}, 'Devicename': {'OS': 'version', 'Name': 'name'}})

Formatting Data

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

Writing To File

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

解决方案

Loop through the dictionary to access its contents. You can use the items() dictionary method to loop through its keys and values together:

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

这篇关于用嵌套字典格式化字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆