Question: I wrote this simple Python script that searches for duplicate IP addresses in an XML document. It outputs where it was found, usually: member. But
I wrote this simple Python script that searches for duplicate IP addresses in an XML document. It outputs where it was found, usually: "member". But I need it to show the full location of where it was found. Unfortunately I can't show you the XML file. The file doesn't really matter though. I just need it to spit out
import xml.etree.ElementTree as ElementTree import sys import glob orig_stdout = sys.stdout f = open('DuplicateOutput.txt', 'w') sys.stdout = f fileList = glob.glob('*.xml') filename = " ".join(fileList) def print_elem(element): return "<%s>" % element.tag tree = ElementTree.parse(filename) root = tree.getroot() chunks = {} iter = root.findall('.//*') for element in iter: if element.text in chunks: chunks[element.text].append(element) else: chunks[element.text] = [element,] for text in chunks: if ((('.') in text) and (('_') in text)) or ((('.') in text) and (('-') in text)): if len(chunks[text]) > 3: print ("\"%s\" is a duplicate object: found in %s" % (text, map(print_elem, chunks[text]))) print "" print(root.tag); sys.stdout = orig_stdout f.close() Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
