我想向此节点附加多个值(如果发生),但是当我执行此操作时,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
最佳答案
您可以使用列表
或元组
来存储多个项目:
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
id2
到一个 str
,并在您使用的地方解析它:
商店:
othermeta.attrib['content'] = ','.join([meta_productid1, meta_productid2])
用途:
id1, id2 = othermeta.attrib['content'].split(',')
希望我对你的问题没有误解。
关于python - python中插入节点替换前一个节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55570933/