You may want to look at some Python Cookbook recipes, such as http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52257 "Auto-detect XML encoding" by Paul Prescod