Python lxml - 查找标记块修正
Python lxml - find tag block ammend
我打开并解析了以下 xml,现在我需要找到具有领土 'IE' 的特定产品块,然后修改其 'cleared_for_sale' 和 'wholesale_price_tier' 值,但我不确定该怎么做。这是不起作用的:
a = 0
territory = "IE"
for products22 in tree.xpath("//video/products/product"):
node_video_temp = tree.xpath('//video/products/product')[a]
if root.iterfind(node_video_temp, territory):
## Update the values ##
a +=1
这是 xml:
<?xml version="1.0" encoding="UTF-8"?>
<package xmlns="http://apple.com/itunes/importer" version="film5.0">
<video>
<products>
<product>
<territory>GB</territory>
<cleared_for_sale>true</cleared_for_sale>
<wholesale_price_tier>1</wholesale_price_tier>
</product>
<product>
<territory>IE</territory>
<cleared_for_sale>true</cleared_for_sale>
<wholesale_price_tier>1</wholesale_price_tier>
</product>
<product>
<territory>US</territory>
<cleared_for_sale>true</cleared_for_sale>
<wholesale_price_tier>1</wholesale_price_tier>
</product>
</products>
</video>
</package>
您可以创建一个 xpath 表达式来获取 territory
为 IE
的所有产品:
//product[territory = "IE"]
但是,您需要handle an empty namespace这里:
from lxml import etree
data = """<?xml version="1.0" encoding="UTF-8"?>
<package xmlns="http://apple.com/itunes/importer" version="film5.0">
<video>
<products>
<product>
<territory>GB</territory>
<cleared_for_sale>true</cleared_for_sale>
<wholesale_price_tier>1</wholesale_price_tier>
</product>
<product>
<territory>IE</territory>
<cleared_for_sale>true</cleared_for_sale>
<wholesale_price_tier>1</wholesale_price_tier>
</product>
<product>
<territory>US</territory>
<cleared_for_sale>true</cleared_for_sale>
<wholesale_price_tier>1</wholesale_price_tier>
</product>
</products>
</video>
</package>
"""
ns = {"x": "http://apple.com/itunes/importer"}
territory = 'IE'
root = etree.fromstring(data)
for product in root.xpath('//x:product[x:territory = "%s"]' % territory, namespaces=ns):
print product.findtext('x:cleared_for_sale', namespaces=ns)
print product.findtext('x:wholesale_price_tier', namespaces=ns)
为领土打印 cleared_for_sale
和 wholesale_price_tier
="IE":
true
1
我打开并解析了以下 xml,现在我需要找到具有领土 'IE' 的特定产品块,然后修改其 'cleared_for_sale' 和 'wholesale_price_tier' 值,但我不确定该怎么做。这是不起作用的:
a = 0
territory = "IE"
for products22 in tree.xpath("//video/products/product"):
node_video_temp = tree.xpath('//video/products/product')[a]
if root.iterfind(node_video_temp, territory):
## Update the values ##
a +=1
这是 xml:
<?xml version="1.0" encoding="UTF-8"?>
<package xmlns="http://apple.com/itunes/importer" version="film5.0">
<video>
<products>
<product>
<territory>GB</territory>
<cleared_for_sale>true</cleared_for_sale>
<wholesale_price_tier>1</wholesale_price_tier>
</product>
<product>
<territory>IE</territory>
<cleared_for_sale>true</cleared_for_sale>
<wholesale_price_tier>1</wholesale_price_tier>
</product>
<product>
<territory>US</territory>
<cleared_for_sale>true</cleared_for_sale>
<wholesale_price_tier>1</wholesale_price_tier>
</product>
</products>
</video>
</package>
您可以创建一个 xpath 表达式来获取 territory
为 IE
的所有产品:
//product[territory = "IE"]
但是,您需要handle an empty namespace这里:
from lxml import etree
data = """<?xml version="1.0" encoding="UTF-8"?>
<package xmlns="http://apple.com/itunes/importer" version="film5.0">
<video>
<products>
<product>
<territory>GB</territory>
<cleared_for_sale>true</cleared_for_sale>
<wholesale_price_tier>1</wholesale_price_tier>
</product>
<product>
<territory>IE</territory>
<cleared_for_sale>true</cleared_for_sale>
<wholesale_price_tier>1</wholesale_price_tier>
</product>
<product>
<territory>US</territory>
<cleared_for_sale>true</cleared_for_sale>
<wholesale_price_tier>1</wholesale_price_tier>
</product>
</products>
</video>
</package>
"""
ns = {"x": "http://apple.com/itunes/importer"}
territory = 'IE'
root = etree.fromstring(data)
for product in root.xpath('//x:product[x:territory = "%s"]' % territory, namespaces=ns):
print product.findtext('x:cleared_for_sale', namespaces=ns)
print product.findtext('x:wholesale_price_tier', namespaces=ns)
为领土打印 cleared_for_sale
和 wholesale_price_tier
="IE":
true
1