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

  • Home
  • SEARCH
  • 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 8546351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:59:53+00:00 2026-06-11T12:59:53+00:00

I am writing some code that uses the Element.find() method from Python’s xml.etree.ElementTree module.

  • 0

I am writing some code that uses the Element.find() method from Python’s xml.etree.ElementTree module. However, I am finding that as soon as I start using str.replace(), Python thereafter interprets my calls to object.find() as str.find() rather than Element.find().

I need my code to grab the text from various Element objects from an XML file, and in some cases I need to modify it with str methods. How can I do this without changing how the interpreter interprets object.find()? Here my code:

import xml.etree.ElementTree as xml
import re
import json

pattern = re.compile('[a-z,0-9,-]+')
c_pattern = re.compile('\]+\]')
c2_pattern = re.compile('\[/caption\]')

tree = xml.parse('file.xml')
root = tree.getroot()

for item in root.iter('item'):
    if item.find('{http://wordpress.org/export/1.2/}post_type').text == 'post':
        t = item.find('{http://purl.org/rss/1.0/modules/content/}encoded').text
        print item.find
        try:
            t = t.encode('utf-8')

            o_cap = re.findall(c_pattern,t)
            e_cap = re.findall(c2_pattern,t)

            for item in o_cap:
                t = t.replace(item,'') 
            for item in e_cap:
                t = t.replace(item,'')           
        except:
            t = 'None'
        print item.find
        title = item.find('title').text

The first print statement returns <bound method Element.find of <Element 'item' at 0x2a7a7d0>>, while the second returns <built-in method find of str object at 0x2b56e00>.

I’ve tried subclassing Element, but I don’t know how to make the Element nodes in my XML file become objects of the new subclass. I’ve also tried simply defining a new method that refers to the Element.find() method, but when I tried to use it the interpreter simply told me that an Element object didn’t have a method by the name of my user-defined one.

Another way I’ve thought of is some kind of

from xml.etree.ElementTree import Element.find() as Element.some_other_name() 

but that doesn’t work (I’ve tried a bunch of syntactic variations and they don’t work either). Does anyone know what I can/should do to get around this issue?

  • 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-06-11T12:59:55+00:00Added an answer on June 11, 2026 at 12:59 pm

    You are clobbering the name item. Try switching the variable name in your inner loop to something else, like poop.

    for poop in o_cap:  
        t = t.replace(poop,'') 
    for poop in e_cap:
        t = alvin.replace(poop,'')  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing some python code to interact with a C DLL that uses structures
I am writing some Python code that uses a library to communicate with an
I'm writing some code that uses some unmanaged calls into user32 functions such as
I'm writing some code that takes a report from the mainframe and converts it
I'm writing some code on a mobile device that uses a REST service to
I am writing some code in VB.NET that uses a switch statement but in
I'm thinking of writing some simple static code analysis that uses class declarations and
I've been writing WinForms code that uses a BackgroundWorker to do some async work,
I am writing some code for a website that uses 4 list items to
I'm writing Cocoa code that uses some classes defined in a C++ library. I

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.