Python 如何合并id上的两个词典并将它们写入xml文件

Python 如何合并id上的两个词典并将它们写入xml文件,python,xml,Python,Xml,我有不同对象的产品信息、产品说明和产品亮点。这两个对象都有product_id,因此我可以将它们关联在一起 description\u items是一个字典列表,例如: [ {'product_id': '123', 'description': 'desc1', 'price': '$40' }, {'product_id': '124', 'description': 'desc2', 'price': '$50' }, {'product_id': '125', 'descripti

我有不同对象的产品信息、产品说明和产品亮点。这两个对象都有product_id,因此我可以将它们关联在一起

description\u items
是一个字典列表,例如:

[
 {'product_id': '123', 'description': 'desc1', 'price': '$40' },
 {'product_id': '124', 'description': 'desc2', 'price': '$50' },
 {'product_id': '125', 'description': 'desc3', 'price': '$99' },
] 
product\u highlight\u dict
是(product\u id,
ProductHighlight
)的字典

我要做的是合并这两种类型并将它们写入xml文档,在以下代码中,我可以合并这两种类型:

for description_item in self.decription_items:
    product_id = .get('product_id')
        if product_id:
            product_highlight = spider.product_highlight_dict.get(product_id)
            # I don't know how to combine description_item and 
            # product_highlight and write them to an xml
更新 我使用以下代码将
product\u highlight\u dict
写入xml。我不知道如何在下面的逻辑中包含
description\u项

    highlights = []
    for k in self.product_highlight_dict:
        highlights.append(vars(self.product_highlight_dict[k]))

    xml = dicttoxml.dicttoxml(highlights, custom_root='product_highlights')
    file = open('filename', "wb")
    file.write(xml)
    file.close()

您可以使用
description\u items
为每个产品建立一个包含
description
price
信息的字典:

product_data={}
对于描述项中的描述项:
产品标识=描述项目[“产品标识”]
产品数据[产品id]=描述项目
然后你可以在你的代码中像这样使用它

highlights=[]
对于产品标识,在self.product\u highlight\u dict.items()中突出显示产品:
突出显示=变量(产品突出显示)
如果产品数据中的产品id:
突出显示。更新(产品数据[产品id])
突出显示。附加(突出显示)
for description_item in self.decription_items:
    product_id = .get('product_id')
        if product_id:
            product_highlight = spider.product_highlight_dict.get(product_id)
            # I don't know how to combine description_item and 
            # product_highlight and write them to an xml
    highlights = []
    for k in self.product_highlight_dict:
        highlights.append(vars(self.product_highlight_dict[k]))

    xml = dicttoxml.dicttoxml(highlights, custom_root='product_highlights')
    file = open('filename', "wb")
    file.write(xml)
    file.close()