September 10, 2014

How to Manipulate XML Pythonically

I just submitted my first pypi package, xickle. Letting you persist and read from XML just as easily as you manipulate json using the json module. Indeed, the method signatures are the same. Example code:

toxml = {True: '1', False: 0} # Dictionary
# To dump to a file
import xickle
xickle.dump(toxml, filename)

# To dump to a string containing the well-formed xml
import xickle
xickle.dumps(toxml)

# To read in from a file
import xickle
dictionary = xickle.load(filename)

# To read in from a string
import xickle
dictionary = xickle.loads(xmlstring)
No, persistence isn't pretty. Then again, no matter what James says, XML was not meant to be read by humans. It's meant to be read by machines.

No comments:

Post a Comment