gpt4 book ai didi

python - 排列嵌套的 dict 项 Python

转载 作者:太空宇宙 更新时间:2023-11-04 10:00:06 26 4
gpt4 key购买 nike

这段代码我已经写好了,但是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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com