copying all data(tags and values) after a particular XML tag

Chris cwitts at gmail.com
Thu Mar 13 07:43:50 CET 2008


On Mar 13, 8:21 am, bije... at gmail.com wrote:
> i've an XML file with the following structure....
>
> <a>
> <b>
> <c>
> .
> .
> .
> .
> .</c>
> </b>
> </a>
>
> what i want to do is copy all data(tags and all) between N and N+k
> appearances of <c>. I am a python newbie. How do I do it?
>
> Thanks.

You can take a look at the docs for Beautiful Soup, they might help
you. If it's just as simple as you're describing and you could always
do something like...

test = """<a>
    <b>
    <c>
    1
    </c>
    </b>
    <b>
    <c>
    2
    </c>
    </b>
    </a>"""

test = test.replace('<c>','|||').replace('</c>','|||')
[i for (j,i) in enumerate(tmp2.split('|')) if j%2]

which would yield
['\n    1\n    ', '\n    2\n    ']

which you could then parse as required.



More information about the Python-list mailing list