stripping fields from xml file into a csv

2010-02-27 Thread Hal Styli
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.

Thanks for any constructive help given.

Hal
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: stripping fields from xml file into a csv

2010-02-28 Thread Hal Styli
On 28 Feb, 19:20, Stefan Behnel  wrote:
> Hai Vu, 28.02.2010 17:41:
>
> > By the way, Stefan, I am using Python 2.6. Do you know the differences
> > between ElementTree and cElementTree?
>
> Use cElementTree, it's implemented in C and a lot faster and more memory
> friendly.
>
> http://effbot.org/zone/celementtree.htm#benchmarkshttp://codespeak.net/lxml/performance.html
>
> Stefan

Thanks for the responses so far, most enlightening.

Stefan, I was happy to see such concise code.
Your python worked with only very minor modifications.

Hai's test xml data *without* the first and last line is close enough
to the data I am using:







... quirky.

I  get a large file given to me in this format. I believe it is
created by something like:
grep 'customer=' *.xml, where there are a large number of  xml files.

I had to edit the data to include the first and last lines, 
and ,
to get the python code to work. It's not an arduous task(!), but can
you recommend a way to get it to work without
manually editing the data?

One other thing, what's the Roland Mueller post above about (I'm
viewing htis in google groups)? What would the test.xsl file look
like?

Thanks again

Hal.
-- 
http://mail.python.org/mailman/listinfo/python-list