Created
November 2, 2012 16:25
-
-
Save tierra/4002440 to your computer and use it in GitHub Desktop.
Sort Pidgin Groups
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
#!/usr/bin/python | |
import xml.etree.ElementTree as ET | |
tree = ET.parse('blist.xml') | |
container = tree.find('blist') | |
def getkey(elem): | |
return elem.attrib['name'] | |
container[:] = sorted(container, key=getkey) | |
tree.write('blist.xml') |
Thank you, although it didnt work right way, it pointed me to the right direction.
#!/usr/bin/python3
import xml.etree.ElementTree as ET
tree = ET.parse('blist.xml')
container = tree.find('blist')
def getkey(elem):
if 'name' in elem.attrib:
return elem.attrib['name']
else:
return ''
container[:] = sorted(container, key=getkey)
xml_string = ET.tostring(tree.getroot(), encoding='unicode')
print(xml_string)
tree.write('blist-ordered.xml')
As I'm not confident with python, I wrote to blist-ordered.xml first, then moved that to the original blist.xml ahaha
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks! Just what I was looking for 👍