stripping fields from xml file into a csv

John Bokma john at castleamber.com
Sat Feb 27 21:56:22 CET 2010


Hal Styli <sillyhat at yahoo.com> writes:

> Hello,
>
> Can someone please help.
> I have a sed solution to the problems below but would like to rewrite
> in python...
>
> I need to strip out some data from a quirky xml file into a csv:
>
> from something like this
>
> < ..... cust="dick" .... product="eggs" ... quantity="12" .... >
> < .... cust="tom" .... product="milk" ... quantity="2" ...>
> < .... cust="harry" .... product="bread" ... quantity="1" ...>
> < .... cust="tom" .... product="eggs" ... quantity="6" ...>
> < ..... cust="dick" .... product="eggs" ... quantity="6" .... >
>
> to this
>
> dick,eggs,12
> tom,milk,2
> harry,bread,1
> tom,eggs,6
> dick,eggs,6
>
> I am new to python and xml and it would be great to see some slick
> ways of achieving the above by using python's XML capabilities to
> parse the original file or python's regex to achive what I did using
> sed.

It's not clear how your XML actually looks, but (especially) if those
are all attributes of one element I probably would just use xml.sax

I strongly suggest to not use regex to parse XML.

-- 
John Bokma                                                               j3b

Hacking & Hiking in Mexico -  http://johnbokma.com/
http://castleamber.com/ - Perl & Python Development



More information about the Python-list mailing list