插入节点替换python中以前的节点
我想将多个值附加到此节点(如果有),但执行此操作时,productid2将替换productid1。是否有方法返回单独的产品id项目插入节点替换python中以前的节点,python,xpath,Python,Xpath,我想将多个值附加到此节点(如果有),但执行此操作时,productid2将替换productid1。是否有方法返回单独的产品id项目 for node in tree.xpath( '//map/topicmeta' ): node.insert(5, othermeta) othermeta.set("name", "product-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)
或
您可以通过以下方式获得:
id1, id2 = othermeta.attrib['content']
如果
othermeta.attrib['content']
的类型被限制为str
,您可以将id1
id2
压缩为一个str
,并在您使用的地方解析它:
商店:
othermeta.attrib['content'] = ','.join([meta_productid1, meta_productid2])
使用:
希望我没有误解您的问题。您可以使用
列表
或元组
存储多个项目:
othermeta.attrib['content'] = []
othermeta.attrib['content'].append(meta_productid1)
othermeta.attrib['content'].append(meta_productid2)
或
您可以通过以下方式获得:
id1, id2 = othermeta.attrib['content']
如果
othermeta.attrib['content']
的类型被限制为str
,您可以将id1
id2
压缩为一个str
,并在您使用的地方解析它:
商店:
othermeta.attrib['content'] = ','.join([meta_productid1, meta_productid2])
使用:
希望我对你的问题没有误解。谢谢你的帮助。不幸的是,这样做我得到了错误:参数必须是字节或Unicode,得到了“列表”我更新了一个新方法,
join
和split
,你可以试试这个。谢谢你的帮助。不幸的是,这样做会导致错误:参数必须是字节或Unicode,得到“列表”我更新了一个新方法,join
和split
,您可以尝试此操作。一个节点可能只有一个名称-创建两个节点一个节点可能只有一个名称-创建两个节点