第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 id 上合并 2 個字典并將它們寫入 xml 文件

如何在 id 上合并 2 個字典并將它們寫入 xml 文件

元芳怎么了 2023-03-08 16:08:29
我有不同對象的產(chǎn)品信息、產(chǎn)品描述和產(chǎn)品亮點。這兩個對象都有 product_id,所以我可以將它們關(guān)聯(lián)在一起。decription_items是字典列表,例如:[ {'product_id': '123', 'description': 'desc1', 'price': '$40' }, {'product_id': '124', 'description': 'desc2', 'price': '$50' }, {'product_id': '125', 'description': 'desc3', 'price': '$99' },] product_highlight_dictProductHighlight是 (product_id, )的字典{ '123': <product_123_highligh>, '124': <product_124_highligh>, '125': <product_125_highligh>,}最后ProductHighlight是一個類:class ProductHighlight:    def __init__(self, product_id, location, area):        self.product_id = product_id        self.location = location        self.area = area我想做的是合并這兩種類型并寫入一個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_highlight_dictxml。我不知道如何包含description_item在以下邏輯中?    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()
查看完整描述

1 回答

?
慕虎7371278

TA貢獻1802條經(jīng)驗 獲得超4個贊

您可以使用每個產(chǎn)品的信息構(gòu)建description_items字典:descriptionprice


product_data = {}

for description_item in description_items:

    product_id = description_item["product_id"]

    product_data[product_id] = description_item

然后你可以像這樣在你的代碼中使用它


highlights = []

for product_id, product_highlight in self.product_highlight_dict.items():

    highlight = vars(product_highlight)

    if product_id in product_data:

        highlight.update(product_data[product_id])

    highlights.append(highlight)


查看完整回答
反對 回復(fù) 2023-03-08
  • 1 回答
  • 0 關(guān)注
  • 82 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號