python - 插入节点替换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
解决方案
您可以使用list
ortuple
来存储多个项目:
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']
如果 type ofothermeta.attrib['content']
被限制为str
,你可以连接id1
id2
到 one str
,并在你使用的地方解析它:
店铺:
othermeta.attrib['content'] = ','.join([meta_productid1, meta_productid2])
利用:
id1, id2 = othermeta.attrib['content'].split(',')
希望我对你的问题没有误解。
推荐阅读
- android - Android:如何录制视频并将其实时发送到服务器
- list - Flutter - 明智地分组日期并显示在粘性标题下
- terraform - 当父资源计数为零时,depends_on 资源仍会尝试运行
- javascript - 如何根据商品数量计算每件商品的价格?
- arrays - 在参数声明中指定数组的大小有什么意义?
- python - Altair 颜色分级值
- python - 如何在python中为模块设置日志记录环境
- excel - VBA中两个二维数组之间的区别
- javascript - JavaScript - 如何遍历对象数组
- python - 构建具有任意层数的 Keras 模型的函数