0

Okay. I know this is duplicate.

However, I am a decent programmer, I have been trying this for 2 hours and got nothing!!

The top several lines of the xml file looks like the following.

<response>
    <lst name="responseHeader">
    <int name="status">0</int>
    <int name="QTime">2</int>
    <lst name="params">
        <str name="d">100</str>
        <str name="sort">score asc</str>
        <str name="fl">
                        .
                        .
                        .

Here, what I want to get is just the number between <int name="QTime"> and </int>, which is 2 in this case.

How can I do this? I've used ElementTree, and the documentation is awful.

0

1 Answer 1

1

Using lxml.etree, and assuming <response> is the document root, you can do this:

import lxml.etree
xml = lxml.etree.parse(<file-like object>)
root = xml.getroot()
elements = root.xpath("int[@name='QTime']")
values = [int(x.text.strip()) for x in elements]

where <file-like object> can be an opened file object or a StringIO::StringIO object, etc...

elements will be a list of <int name="QTime"> elements. values will be the corresponding list of integer values.

If <response> is not the document root, you can use //response/int[@name='QTime'] in xpath instead.

Sign up to request clarification or add additional context in comments.

1 Comment

//int scans all elements, is slower, and can return more results than intended. Assuming <response> is the top level element in the tree, 'int[@name="QTime"]'

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.