XML/encoding/prolog/python hell...

Stefan Behnel stefan.behnel-n05pAM at web.de
Sat Apr 14 12:53:50 CEST 2007


fscked schrieb:
> I am a beginning pythoner and I am having a terrible time trying to
> figure out how to do something that (it would seeme to me) should be
> fairly simple.
> 
> I have a CSV file of unknown encoding and I need to parse that file to
> get the fields <--- DONE
> I need to create an xml document that has the proper prolog and
> namespace information in it. <--- NOT DONE
> I need it to be encoded properly<--- Looks right in IE, not right in
> any other app.

UTF-8 encoding is the default. No need for a prologue here.

ET 1.3 will have an xml_declaration keyword argument for write() that will
allow you to write the declaration even if unnecessary. lxml already has it
now (and is ET compatible, so your code should just straight work).

http://codespeak.net/lxml


> I should say that I have googled my butt off, tried ElementTree,
> CSV2XML, and various other things and cannot get any of them to work.
> 
> A sample of the output I am looking for is as follows:
> 
> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
> <ns2:boxes xmlns:ns2="Boxes">
> 	<ns2:box id="9" mac="333333" d_a="2006" hw_ver="v1.1" sw_ver="3"
> pl_h="No Data" name="Lounge" address="here" phone="555-5555"
> country="US" city="LA"/>
> 	<ns2:box id="7" mac="444444" d_a="2005" hw_ver="v1.0" sw_ver="3"
> pl_h="No Data" name="MyHouse" address="there" phone="555-5556"
> country="US" city="New York"/>
> </ns2:boxes>

This should help you to get namespaces working:

http://effbot.org/zone/element.htm#xml-namespaces

Hope it helps,
Stefan



More information about the Python-list mailing list