我试图使用findall对一些xml元素进行选择,但是没有得到任何结果。在
import xml.etree.ElementTree as ET
import sys
storefront = sys.argv[1]
xmlFileName = 'promotions{0}.xml'
xmlFile = xmlFileName.format(storefront)
csvFileName = 'hrz{0}.csv'
csvFile = csvFileName.format(storefront)
ET.register_namespace('', "http://www.demandware.com/xml/impex/promotion/2008-01-31")
tree = ET.parse(xmlFile)
root = tree.getroot()
print('------------------Generate test-------------\n')
csv = open(csvFile,'w')
n = 0
for child in root.findall('campaign'):
print(child.attrib['campaign-id'])
print(n)
n+=1
XML如下所示:
^{pr2}$你知道我做错了什么吗? 我尝试过stackoverflow上找到的不同解决方案,但似乎没有什么对我有用(从我尝试过的东西来看)。 列表为空。 很抱歉,如果这是非常明显的事情,我是python新手。在
正如@MartijnPieters所提到的here,etree的}用于树的xml输出。因此,考虑使用显式前缀映射默认名称空间。以下使用doc,但甚至可以是cosmin。在
.findall
使用名称空间参数,而{另外,考虑}甚至是
with
和{csv
模块作为打印和CSV输出的更好的处理程序。在相关问题 更多 >
编程相关推荐