Skip to content

Instantly share code, notes, and snippets.

@tierra
Created November 2, 2012 16:25
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tierra/4002440 to your computer and use it in GitHub Desktop.
Save tierra/4002440 to your computer and use it in GitHub Desktop.
Sort Pidgin Groups
#!/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')
@NeoPolus
Copy link

Thanks! Just what I was looking for 👍

@tiekookeit
Copy link

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')

@tiekookeit
Copy link

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