python - python中插入节点替换前一个节点

标签 python xpath

我想向此节点附加多个值(如果发生),但是当我执行此操作时,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/

相关文章:

python - 使用 GLV 中的本地 TinkerGraph

java - XML解析问题

javascript - XPath 迭代异常 : "An attempt was made to use an object that is not, or is no longer, usable"

python - DTW 集群适当数量的指标

python - 如何测试 Python readline 完成?

Pythonic 多态参数

xpath - XSLT-2.0:输出到文件

java - 在 Selenium 中获取文本()不返回文本

PHP 用 DOM 解析(无结果)

python - Django session 管理