python - 排列嵌套的 dict 项 Python

标签 python defaultdict

这段代码我已经写好了,但是dict的输出不是我想要的。这是代码:

class EbExportCustomer(models.Model):
    _inherit = 'res.partner'

    @api.one
    def get_pa_data(self):
        aValues = defaultdict(dict)
        aValues['partner_id'] = self.id
        aValues['name'] = self.name
        aValues['street'] = self.street
        aValues['street2'] = self.street2
        aValues['zip'] = self.zip
        aValues['city'] = self.city
        aValues['country'] = self.country_id.name
        aValues['state'] = self.state_id.name
        aValues['email'] = self.email
        aValues['website'] = self.website
        aValues['phone'] = self.phone
        aValues['mobile'] = self.mobile
        aValues['fax'] = self.fax
        aValues['language'] = self.lang
        aValues['child_ids']['name'] = []
        aValues['child_ids']['function'] = []
        aValues['child_ids']['email'] = []


        if self.child_ids:
            for child in self.child_ids:
                aValues['child_ids']['name'].append(child.name)
                aValues['child_ids']['function'].append(child.function)
                aValues['child_ids']['email'].append(child.email)

        return aValues

我目前正在使用 dicttoxmlcollections.defaultdict ,输出是这样的:

 <Partner><item>
 <website>http://www.chinaexport.com/</website>
 <city>Shanghai</city>
 <fax>False</fax>
 <name>China Export</name>
 <zip>200000</zip>
 <mobile>False</mobile>
 <country>China</country>
 <street2>False</street2>
 <child_ids>
  <function>
   <item>Marketing Manager</item>
   <item>Senior Consultant</item>
   <item>Order Clerk</item>
   <item>Director</item>
  </function>
 <name>
  <item>Chao Wang</item>
  <item>David Simpson</item>
  <item>Jacob Taylor</item>
  <item>John M. Brown</item>
 </name>
<email><item>chao.wang@chinaexport.example.com</item>      \ <item>david.simpson@epic.example.com</item><item>jacob.taylor@millennium.example.com</item><item>john.brown@epic.example.com</item></email></child_ids><phone>+86 21 6484 5671</phone><state>False</state><street>52 Chop Suey street</street><language>en_US</language><partner_id>9</partner_id><email>chinaexport@yourcompany.example.com</email>

但我需要 child_ids 的输出就像:

<child_id>
< function > 
 Marketing
Manager 
</function>
< name >Chao
Wang  </ name >
< email >  chao.wang @ chinaexport.example.com </ email>
</child id>

然后是另一个 <child id>与来自所有其他子 ID 的字段。 提前致谢。

最佳答案

您需要一个列表(可能是字典),而不是三个平行列表。像这样:

aValues['child_ids'] = []


for child in self.child_ids:
    aValues['child_ids'].append({
        'name': child.name,
        'function': child.function,
        'email': child.email
    })

关于python - 排列嵌套的 dict 项 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44001437/

相关文章:

python - 通过 python 创建一个 vlan 并将其添加到网络命名空间

python - 将 jupyter lab notebook 转换为脚本而不添加注释和单元格之间的新行

python - 运行 Python 脚本后在给定目录中的 cd 终端?

Python语法: mutating/reducing a defaultdict?

python - 从 defaultdict 获取原始 key 集

python - 使用字典查找文本文件中出现次数最多的单词

Python 3 : win32com object. 大小抛出异常 (pywintypes.com_error)

python - 过滤掉两个嵌套字典之间不共享的键

不插入缺失值的Python defaultdict

Python defaultdict 深层嵌套数据结构