简体   繁体   English

安排嵌套的字典项Python

[英]Arrange nested dict items Python

I have written this code and it is fine, but the output of the dict is not what i want. 我已经编写了这段代码,这很好,但是dict的输出不是我想要的。 Here is the code: 这是代码:

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

I am currently using dicttoxml and collections.defaultdict , The output is this: 我当前正在使用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>

But i would need the output for child_ids to be like: 但是我需要child_ids的输出是这样的:

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

And then another <child id> with the fields from all the other child ids. 然后是另一个<child id> ,其中包含所有其他子id的字段。 Thanks in advance. 提前致谢。

You want a single list (of dictionaries, probably), rather than three parallel lists. 您只需要一个列表(可能是字典),而不是三个平行列表。 Something like this: 像这样:

aValues['child_ids'] = []


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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM