Python to XML to Python conversion
thehaas at binary.net
thehaas at binary.net
Thu Jul 11 21:22:17 EDT 2002
Mark <markvkane at hotmail.com> wrote:
> Recently my employer has asked me to do some computer work though I'm
> just savvy enough to write minor programs of little significance. My
> job is to write a program that will be generic enough to take any form
> of Python dictionary and be able to convert it to XML and back.
like -- why? If he wants to save the data from one session to the next,
the Pickle or cPickle module will be 100x easier.
> Ladies and gentlemen I am completely at a loss, I'm not a good
> programmer. If anyone one has any good, simple and complicated
> examples, or tips I would be eternally greatful if you'd share them
> with me. I have searched desperately through every posting I could
> find on the subject, but to no avail. Please Help!
The problem isn't very hard . . . no deep Python magic.
I'd do the Python -> XML like this:
outfile = file("out.xml")
outfile.write("<pydict>")
for key in dict.keys():
outfile.write("<%s>%s</%s>\n" %(key, dict[key], key) )
outfile.write("</pydict>")
outfile.close()
How's that?? Well-formed XML, without any DOM-overhead.
If you didn't understand very in of the last sentence, than you better
look at the Python docs (www.python.org/doc). Look for "minidom".
Heck, look up XML.
I'll leave the second problem to you . . . .
-- mikeh
More information about the Python-list
mailing list