-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOutput.py
More file actions
60 lines (47 loc) · 1.91 KB
/
Output.py
File metadata and controls
60 lines (47 loc) · 1.91 KB
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
"""
Writes data lists from an ABIF to a human-readable file format
"""
import random # Used only for testing func, remove in final
from xml.dom.minidom import getDOMImplementation
import types # Used only in assertions
def write_xml(tag_list, filename):
"""
toXML(): Creates an XML-style document from the tags and their values
:param tag_list: Tag dict from ABIF reader to process
:param filename: Name of file to write to, no extension
:return: None
"""
assert len(filename) > 0, \
"Filename cannot be empty."
assert isinstance(tag_list, types.DictionaryType), \
"Dictionary of lists required."
print "Writing", filename + ".xml...",
# Setup DOM Object and list
impl = getDOMImplementation()
doc = impl.createDocument(None, "ABIF", None)
top_element = doc.documentElement
# For each tag in the dict
for tag_name in tag_list:
new_elem = doc.createElement(str(tag_name)) # Create a new element for the tag
# For each item in the list
for item in tag_list[tag_name]:
value_node = doc.createElement("value")
text_item = doc.createTextNode(str(item))
value_node.appendChild(text_item)
new_elem.appendChild(value_node)
# Add the new element and its children to the tree
top_element.appendChild(new_elem)
# Output contents to file
filename += ".xml"
file_writer = open(filename, 'wb')
doc.writexml(file_writer, indent='\t\n', addindent='\t', encoding='utf-8')
file_writer.close()
print "Done!"
def test_xml():
tag_list = {"Tag1": 0, "Tag2": 0, "Tag3": 0, "Tag4": 0, "Tag5": 0, "Tag6": 0}
for tag in tag_list:
values = []
for val in range(10): # Add 10 values to the tag's list of values
values.append(str(random.randrange(1, 100, 15))) # Append random values
tag_list[tag] = values
write_xml(tag_list, "test")