Good afternoon, I am new to python and I have created a program to make a price list based on an xml.
What I am looking to achieve is that only the products that are not on the deactivated list are saved to continue with the process of creating said list, my code is as follows.
import xml.etree.ElementTree as ET
import settings
from utiles import quit_desactivate as quit_desactivate
PRODUCTS = []
tree = ET.parse('prices_list.xml')
root = tree.getroot()
for neighbor in root.findall('PRODUCT'):
PRODUCT_NUMBER = neighbor.find('PRODUCT_NUMBER').text.split('-')
PRICE = neighbor.find('PRICE').text
DESACTIVATE=quit_desactivate(PRODUCT_NUMBER[0])
if DESACTIVATE == False:
PRODUCTS.append((PRODUCT_NUMBER[0],PRICE))
this is the quit_deactivate() function
import settings
def quit_desactivate(product):
agrega = True
for desactivado in settings.DEACTIVATED:
if product==desactivado:
agrega=False
else:
agrega=True
print('id '+product+' resultado:'+str(agrega)) //aqui lo imprimi para validar que lo haga
return agrega
the list where the deactivated ones are is the following.
DEACTIVATED = ['MO6122','MO9489','MO9086','MO9085','MO9261','MO9260','MO9386','MO9155','MO8906','S55000']
all the code does it well, until it makes the comparison since it tries to validate it with the print but the ones that in theory should not appear continue to appear, as it only validates the last one in the list.
ejemplo de lo que imprime
id MO9155 resultado:True
id S47101 resultado:True
id S54500 resultado:True
id S55000 resultado:False
I hope you can help me, and thank you very much.
You need to get out of
for
it in case you want to deactivate it.By going through the entire list and not leaving the
for
, theagregar
one that counts is the last comparison.A more idiomatic way would be like this
You also have a logic error
DESACTIVATE
shouldn't it be calledactive
?you can do it like this:
this will return True or False
now to validate it would only be to do
if you want to use a for: