[KLUG Members] Python XML to CSV
Mark Jones
mj.klug at ccagent.com
Wed Feb 1 01:51:53 EST 2006
I highly suggest AWK. It's awesome for these type of problems. My awk
is a very rusty but I think it would be something like.
\<c99\>field\<\/c99\> { print field ","; }
println;
The /'s are delimiters and the \'s are escapes to tell awk and sed to
use the next character literally. I may have them reversed.
Basically, I've told it to search for a <, a letter and two numbers,
then create a field with everything to the next search of a <, /a letter
and two numbers. Print the field with a ",". When no more can be found
on this line then print a crlf.
You might also use awk's little brother sed to do a global search and
replace on the pattern (<, maybe another character, a letter and two
numbers); replacing the pattern with a comma. The regex should look like
g\/<?c99/>\,\. Then do g\/<row/</,\\. Basically replace the <row> and
created initial comma with nothing. The leading "g" is for global, it
might need to be on the end.
HTH,
Mark
On Mon, 2006-01-30 at 16:09 -0500, Adam Tauno Williams wrote:
> Does anyone have an example for convering an XML document to a CSV, or
> something similiar, in Python?
>
> For instance, I want to turn -
> <row><c01>2</c01><c02>22302101</c02><c03>022</c03><c04>00</c04><c05>01/25/2006</c05><c06>5</c06><c07/><c08/><c09>2.00</c09><c10>277202</c10><c11/><c12>277202 WHEEL,LOAD</c12><c13>65.03</c13><c14>130.06</c14><c15/><c16/><c17>Y</c17><c18>NPP40</c18><c19>2CL02277</c19><c20/><c21/><c22/><c23/><c24/></row>
> -
> into a 24 field CSV file (with a couple of caveats, but I think I can
> figure those out). I want to do it in Python as this example exists
> inside BIE which has an internal Python-script action.
> _______________________________________________
> Members mailing list
> Members at kalamazoolinux.org
>
More information about the Members
mailing list