0

I'm writing a script that is supposed to remove parent elements from the XML file if the child element matches an element in a CSV file. The loops and if statements are working correctly, however when I add the remove, it just deletes everything out of the table regardless of if it matches or not. I can't seem to figure out why it is doing this.

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')
11
  • shouldn't this tree.write('please-work.xml') be outside the loops? Commented Dec 22, 2016 at 17:24
  • please share the xml structure and variable initialization for product and root and tree Commented Dec 22, 2016 at 17:25
  • @VikashSingh example xml structure (without headers): <price-table product-id="tr-33373"> <amount quantity="1">11.99</amount> </price-table> Commented Dec 22, 2016 at 17:28
  • @VikashSingh so each product has a price table. i'm trying to set it up so that if the product-id is equal to a product id in a csv document, it deletes that specific price table. if it is not equal, it is left alone. Commented Dec 22, 2016 at 17:29
  • @VikashSingh in this scenario, tree.write('please-work.xml') can be placed outside the loop for speed's sake. Commented Dec 22, 2016 at 17:31

1 Answer 1

1

In your code, you get all product ids form xml and compare them with each id in your csv-file. If any matches, you remove every element from root.

Your code is equivalent to this:

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')

which is equivalent to this:

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')

You should compare only the current product-id which each id of the csv-file:

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)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.