-
Notifications
You must be signed in to change notification settings - Fork 7
/
xml_feature_parser.py
87 lines (71 loc) · 2.18 KB
/
xml_feature_parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python3
"""XML feature parser[WIP]
This script will parse an XML for a single feature and return a list via terminal
or file, if an output is supplied.
"""
__version__ = "0.2.0"
__status__ = "Production"
import sys
import argparse
import xml.etree.ElementTree as ET
def usr_args():
"""Program Arguments
All arguments for process are defined here.
"""
# initialize parser
parser = argparse.ArgumentParser()
# set usages options
parser = argparse.ArgumentParser(
prog='XML feature parser',
usage='%(prog)s [options]')
# version
parser.add_argument(
'-v', '--version',
action='version',
version='%(prog)s ' + __version__)
parser.add_argument('-f', '--file',
required=True,
help="Path for input file. The input file should be an XML file.")
parser.add_argument('-t', '--tag',
help="Tag or featured for extraction from XML file.")
parser.add_argument('-o', '--output',
required=False,
help="Output file. If no output is provided, the results will output to the terminal.")
if len(sys.argv) <= 1:
sys.argv.append('--help')
return parser.parse_args()
def parse_xml(xml_file, output):
"""Parse XML file
Parameters
----------
xml_file: str
file path/name to be parsed
output: str, optional
file path/name for data to be output to
"""
# tag_list = tag.split('/')
items = []
print('start')
try:
count = 0
root = ET.parse(xml_file).getroot()
for item in root.findall('.'):
for run in item.findall('.//DocumentSummary'):
for exp in run.findall('./Runs/'):
items.append(exp.attrib['acc'])
count += 1
if output:
with open(output, 'w', encoding='utf-8') as file:
for item in items:
file.write(item+'\n')
else:
for item in items:
print(item)
except ET.ParseError:
print(xml_file, 'not well-formed')
def main():
"""Main"""
args = usr_args()
parse_xml(args.file, args.output)
if __name__ == "__main__":
main()