Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5984095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:19:31+00:00 2026-05-22T22:19:31+00:00

I am using ElementTree to load up a series of XML files and parse

  • 0

I am using ElementTree to load up a series of XML files and parse them. As a file is parsed, I am grabbing a few bits of data from it ( a headline and a paragraph of text). I then need to grab some file names that are stored in the XML. They are contained in an element called ContentItem.

My code looks a bit like this:

for item in dirlist:
    newsML = ET.parse(item)
    NewsLines = newsML.getroot()
    HeadLine = NewsLines.getiterator("HeadLine")
    result.append(HeadLine)
    p = NewsLines.getiterator("p")
    result.append(p)
    ci = NewsLines.getiterator("ContentItem")
    for i in ci:
        result.append(i.attrib)

Now, if there was only one type of file, this would have been fine, but it contains 3 types (jpg, flv and a mp4). So as I loop through them in the view, it spits them out, but how do I just grab the flv if I only want that one? or just the mp4? They don’t always appear in the same order in the list either.

Is there a way to say if it ends in .mp4 then do this action, or is there a way to do that in the template even?

If i try to do this;

        url = i.attrib
        if url.get("Href", () ).endswith('jpg'):
            result.append(i.attrib)

I get an error tuple object has no attribute endswith. Why is this a tuple? I thought it was a dict?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T22:19:32+00:00Added an answer on May 22, 2026 at 10:19 pm

    You get a tuple because you supply a tuple (the parentheses) as the default return value for url.get(). Supply an empty string, and you can use its .endswith() method. Also note that the element itself has a get() method to retrieve attribute values (you do not have to go via .attrib). Example:

    if i.get('Href', '').endswith('.jpg'):
        result.append(i.attrib)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using ElementTree to parse a XML file. In some fields, there will
Hi I'm using ElementTree to parse out an xml feed from Kuler. I'm only
I have to write an xml file using python standard modules (not using elementtree,
I am trying to use xml.etree.ElementTree to parse responses from eBay finding API, findItemsByProduct.
In Python 2.6 using ElementTree, what's a good way to fetch the XML (as
I am attempting to pull some information from my tnsnames file using regex. I
I am using lxml for parsing xml data. I have to divide the xml
i'm using Python 2.5 and WinXP. i am parsing xml file as below: <Test>
I'm making an xml parser to parse xml reports from different tools, and each
I am having trouble using the attribute XPath Selector in ElementTree, which I should

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.