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?
You are clobbering the name
item. Try switching the variable name in your inner loop to something else, likepoop.