插入节点替换python中以前的节点

2024-09-30 20:22:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我想将多个值附加到此节点(如果有),但执行此操作时,productid2将替换productid1。是否有方法返回单独的产品id项目

           for node in tree.xpath( '//map/topicmeta' ):
                node.insert(5, othermeta)                       
                othermeta.set("name", "product-id")
                othermeta.attrib['content'] = meta_productid1

                othermeta.set("name", "product-id")
                othermeta.attrib['content'] = meta_productid2

Tags: 项目方法nameidnode节点产品content
1条回答
网友
1楼 · 发布于 2024-09-30 20:22:41

您可以使用listtuple存储多个项目:

othermeta.attrib['content'] = []
othermeta.attrib['content'].append(meta_productid1)
othermeta.attrib['content'].append(meta_productid2)

othermeta.attrib['content'] = [meta_productid1, meta_productid2]
othermeta.attrib['content'] = (meta_productid1, meta_productid2)

您可以通过以下方式获得:

id1, id2 = othermeta.attrib['content']

如果othermeta.attrib['content']的类型限制为str,则可以将id1{}合并为一个str,并在使用以下内容的地方对其进行解析:

商店:

othermeta.attrib['content'] = ','.join([meta_productid1, meta_productid2])

使用:

id1, id2 = othermeta.attrib['content'].split(',')

希望我对你的问题没有误解

相关问题 更多 >