I'm trying to open an XML file and parse through it, looking through its tags and finding the text within each specific tag. If the text within the tag matches a string, I want it remove a part of the string or substitute it with something else.
However, it looks like for some reason my "if-statements" are not working. I want it to do something only when the variable "action" equals 'remove' and do something else only when the variable "action" equals 'substitute". However, when "action" equals 'substitute', the if statement performs, along with what's in the elif statement too. Also, the if, elif, and else statements within the second if statement do not seem to work either. Even when end_int does not equal none, what's inside the if statement happens but does not go on to the elif and else statements when "start_int == None" and for the remaining cases.
The mfn_pn variable is a barcode inputted by the user, something similar to ATL-157-1815, DFW-184-8378., ATL-324-3243., DFW-432-2343.
The XML file has the following data:
<?xml version="1.0" encoding="utf-8"?>
<metadata>
<filter>
<regex>ATL|LAX|DFW</regex >
<start_char>3</start_char>
<end_char></end_char>
<action>remove</action>
</filter>
<filter>
<regex>DFW.+\.$</regex >
<start_char>3</start_char>
<end_char>-1</end_char>
<action>remove</action>
</filter>
<filter>
<regex>\-</regex >
<replacement></replacement>
<action>substitute</action>
</filter>
<filter>
<regex>\s</regex >
<replacement></replacement>
<action>substitute</action>
</filter>
<filter>
<regex> T&R$</regex >
<start_char></start_char>
<end_char>-4</end_char>
<action>remove</action>
</filter>
</metadata>
The Python code I'm using is:
from xml.etree.ElementTree import ElementTree
# filters.xml is the file that holds the things to be filtered
tree = ElementTree()
tree.parse("filters.xml")
# Get the data in the XML file
root = tree.getroot()
# Loop through filters
for x in root.findall('filter'):
# Find the text inside the regex tag
regex = x.find('regex').text
# Find the text inside the start_char tag
start_prim = x.find('start_char')
# If the element exists assign its text to start variable
start = start_prim.text if start_prim is not None else None
start_int = int(start) if start is not None else None
# Find the text inside the end_char tag
end_prim = x.find('end_char')
# If the element exists assign its text end variable
end = end_prim.text if end_prim is not None else None
end_int = int(end) if end is not None else None
# Find the text inside the action tag
action = x.find('action').text
if action == 'remove':
if re.match(r'%s' % regex, mfn_pn, re.IGNORECASE):
if end_int == None:
mfn_pn = mfn_pn[start_int:]
elif start_int == None:
mfn_pn = mfn_pn[:end_int]
else:
mfn_pn = mfn_pn[start_int:end_int]
elif action == 'substitute':
mfn_pn = re.sub(r'%s' % regex, '', mfn_pn)
Aucun commentaire:
Enregistrer un commentaire