Python XML ElementTree删除所有元素

时间:2016-12-22 17:15:29

标签: python xml elementtree

如果子元素与CSV文件中的元素匹配,我正在编写一个应该从XML文件中删除父元素的脚本。循环和if语句正常工作,但是当我添加删除时,它只是删除表中的所有内容,无论它是否匹配。我似乎无法弄清楚为什么会这样做。

cs = open('skus.csv', 'rb')
reader = csv.reader(cs)


tree = et.parse('christmas-dog-price.xml')
root = tree.getroot()
xmlns = {'pricebook': '{http://www.demandware.com/xml/impex/pricebook/2006-10-31}'}
price_table = root.find('.//{pricebook}price-table'.format(**xmlns))
product_id = [price_table.get('product-id') for price_table in root]
for sku in reader:
    for product in product_id:
        for price_table in root:
            if sku[0] != product:
                continue
            if sku[0] == product:
                root.remove(price_table)
            tree.write('please-work.xml')

1 个答案:

答案 0 :(得分:1)

在您的代码中,您将获得xml的所有产品ID,并将它们与csv文件中的每个ID进行比较。如果匹配,则从root中删除每个元素。

您的代码与此相同:

for sku in reader:
    for product in product_id:
        if sku[0] == product:
            for price_table in root:
                root.remove(price_table)
tree.write('please-work.xml')

相当于:

if any(sku[0] in product_id for sku in reader):
    for price_table in root:
        root.remove(price_table)
tree.write('please-work.xml')

您应该只比较csv文件的每个id的当前product-id:

with open('skus.csv', 'rb') as cs:
    reader = csv.reader(cs)
    product_ids = [sku[0] for sku in reader]

tree = et.parse('christmas-dog-price.xml')
root = tree.getroot()
xmlns = {'pricebook': '{http://www.demandware.com/xml/impex/pricebook/2006-10-31}'}
price_table = root.find('.//{pricebook}price-table'.format(**xmlns))
to_be_removed = [element for element in price_table if price_table.get('product-id') in product_ids]
for element in to_be_removed:
    root.remove(element)