Greetings I want to take part of a word remove it and then append something new. Here is my xml

<Symbols>

                        <Item>
                                <Symbol>UPS2008</Symbol>
                                <Order>usg_05Y</Order>
                        </Item>

</Symbols>

My python code should remove the 2008 in UPS and change it to 2009 (basically write to file with elementtree), except I do not know what is up with my code. Any help would be appreciated and yes I read the effbot site but I must be doing something wierd. Thank you.

import urllib
import sys, string, os, datetime
import xml.etree.ElementTree as ET

root=ET.parse("thefile").getroot()
iter=root.getiterator('Item')
print iter
for n in iter :
        symbol=n.findtext('Symbol')
        print symbol
        if 'USP' in symbol:
            nodes=symbol[3:7]
            
          n.remove(nodes)
            n.append('2009')
            print "done"

Thanks

Firstly, it is 'UPS' and not 'USP'

Secondly, wouldn't this be simpler if 'UPS2008' is a rather unique string:

xml = """<Symbols>

<Item>
<Symbol>UPS2008</Symbol>
<Order>usg_05Y</Order>
</Item>

</Symbols>"""

new_xml = xml.replace('UPS2008', 'UPS2009')

print new_xml

"""
my output:
<Symbols>

<Item>
<Symbol>UPS2009</Symbol>
<Order>usg_05Y</Order>
</Item>
"""

Hi msaenz,

Lardmeister is correct in that your check for the symbol token "UPS" is flubbed. His approach works fine if you want to treat the whole XML file as a string. However, if you want to do this using the Python xml module, please see my modifications to your code below:

import sys, xml.etree.ElementTree as ET

# Create the root of the ElementTree from file
root = ET.parse("thefile").getroot()

# Get an iterator for the root node
iterator = root.getiterator("Item")

# Loop using the iterator
# (Please use clear variable names!)
for item in iterator:

    # To find a sub-element with a text tag, use find()
    old_symbol = item.find("Symbol")

    # This is how you get its text field
    text = old_symbol.text

    # Is 'UPS' in our text field?
    if 'UPS' in text:

        # If so, remove the sub-element
        # remove() takes out nodes, not text
        item.remove(old_symbol)

        # Add a new sub-element to the item
        new_symbol = ET.SubElement(item, "Symbol")

        # Set its text field to the appropriate thing
        new_symbol.text = text[0:3]+"2009"

# Now get the full tree from the root
tree = ET.ElementTree(root)

# And write to file!
tree.write("out.xml")

You can learn more at the effbot site. This link takes you to the page which discusses searching for sub-elements and adding/removing them. It's not the main page - maybe you missed it?

Hope this helps!

Hi msaenz,

Lardmeister is correct in that your check for the symbol token "UPS" is flubbed. His approach works fine if you want to treat the whole XML file as a string. However, if you want to do this using the Python xml module, please see my modifications to your code below:

import sys, xml.etree.ElementTree as ET

# Create the root of the ElementTree from file
root = ET.parse("thefile").getroot()

# Get an iterator for the root node
iterator = root.getiterator("Item")

# Loop using the iterator
# (Please use clear variable names!)
for item in iterator:

    # To find a sub-element with a text tag, use find()
    old_symbol = item.find("Symbol")

    # This is how you get its text field
    text = old_symbol.text

    # Is 'UPS' in our text field?
    if 'UPS' in text:

        # If so, remove the sub-element
        # remove() takes out nodes, not text
        item.remove(old_symbol)

        # Add a new sub-element to the item
        new_symbol = ET.SubElement(item, "Symbol")

        # Set its text field to the appropriate thing
        new_symbol.text = text[0:3]+"2009"

# Now get the full tree from the root
tree = ET.ElementTree(root)

# And write to file!
tree.write("out.xml")

You can learn more at the effbot site. This link takes you to the page which discusses searching for sub-elements and adding/removing them. It's not the main page - maybe you missed it?

Hope this helps!

thank you both very much for your help. I did not want to treat the whole xml file as a string but attempt to use the module. I guessI just needed it broken down to me a bit more than the effbot site did for me. Thank you once again, with this example above it will help me understand how the module works and what effbot is talking about.

This article has been dead for over six months. Start a new discussion instead.