Skip to content

Instantly share code, notes, and snippets.

@kpsychas
Last active Dec 2, 2015
Embed
What would you like to do?
Count number of labels in a Brain XML file (http://www.thebrain.com/)
#!/usr/bin/env python
'''
Count number of labels in a Brain XML file
(http://www.thebrain.com/)
'''
import xml.etree.ElementTree as ET
if __name__ == '__main__':
tree = ET.parse('MyBrain.xml')
root = tree.getroot()
thoughts = root.find('Thoughts').findall('Thought')
links = root.find('Links').findall('Link')
for thought in thoughts:
label = thought.find('label').text
if label is not None:
lab_guid = thought.find('guid').text
lab_count = 0
for link in links:
try:
idA = link.find('idA').text
idB = link.find('idB').text
if (idA == lab_guid) or (idB == lab_guid):
lab_count += 1
except:
pass
print('{}: {}'.format(label, lab_count))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment