Created
November 10, 2017 23:50
-
-
Save strets123/8aa8eca421971ddda4d6a2762accd5a3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import xml.etree.ElementTree as ET | |
import sys | |
# Main | |
def process(stream, prefix) : | |
items = [] | |
# Parse the XML | |
tree = ET.parse(stream) | |
start = 'TradeMark' | |
# Get root element | |
data = None | |
for elem in tree.iter(): | |
print(elem.tag) | |
if elem.tag.strip() == start: | |
if data: | |
items.append(dict(data)) | |
data = {} | |
if data is not None and elem.text: | |
t = elem.text.strip() | |
if not elem.tag in data: | |
data[elem.tag] = t | |
else: | |
data[elem.tag] = [data[elem.tag]] + [t] | |
print(items[0]) | |
# Linearize | |
# Each argument is a file | |
args = sys.argv[1:] | |
# Loop on files | |
for filename in args : | |
# Open the file | |
file = open(filename) | |
# If we process several files, prefix each one with its path | |
if len(args) > 1 : | |
prefix = filename + ":" | |
else: | |
prefix = "" | |
# Process it | |
process(file, prefix) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment