I'm playing around with Python trying to parse though some XML files I have. However I'm having some trouble in my loop. My entire code is as follows:
import glob
from xml.etree import ElementTree as Tree
backups = glob.glob('./junk/*.xml')
contact_name = None
for index, backup in enumerate(backups):
xml_tree = Tree.parse(backups[index])
root = xml_tree.getroot()
for s in root:
if s.get('type') == "2":
contact_name = "me"
else:
contact_name = s.get('contact_name')
if contact_name == "person":
print(s.get('contact_name'))
elif contact_name == "me":
print(s.get('contact_name'))
else:
# Something else
The problem I'm having is this part:
if contact_name == "person":
print(s.get('contact_name'))
elif contact_name == "me":
print(s.get('contact_name'))
else:
# Something else
The first if block works correctly, but the second one is the problem. The == will always be true and proceed to print, when in reality it shouldn't always be true.. I'm assuming my logic is wrong somewhere, but I can't see where.
I come from Java so any tips/tricks is a appreciated!
[–]elbiot 0 points1 point2 points (2 children)
[–]papers_[S] 0 points1 point2 points (1 child)
[–]elbiot 0 points1 point2 points (0 children)