Python 2.6.1 : expected path separator ([)
Asked Answered
B

1

12

I am getting a path separator error in python 2.6.1. I have not found this issue with python 2.7.2 version, but unfortunately I need this in 2.6.1 only. Is there any another way to achieve the same? :(

my code :-

import xml.etree.ElementTree as ET #version 1.2.6
import sys   

class usersDetail(object): 

    def __init__(self, users=None):
        self.doc = ET.parse("test.xml")
        self.root = self.doc.getroot()

    def final_xml(self,username):
        r = self.root.find("user[@username='user1']") #not working in 2.6.1 :(
        self.root.remove(r)
        print r
        tree = ET.ElementTree(self.root)
        tree.write("msl.xml") 

if __name__ == '__main__':
    parser = usersDetail()
    parser.final_xml("user1") 

test.xml is :-

<?xml version="1.0"?>
<users>
<user afp="yes" cifs="yes"  username="user1" volume="vol" webdev="yes" /></user>
</users>

What this will do is it will remove the xml only if username = username. Thanks in advance for your valuable time.

Borough answered 2/12, 2012 at 9:28 Comment(0)
H
18

You are using an XPath expression, that is not supported by the ElementTree version included in Python 2.6. You'll need to filter for the attribute manually, after a .findall():

def final_xml(self,username):
    users = self.root.findall("user")
    for user in users:
        if user.attrib.get('username') == 'user1':
            break
    else:
        raise ValueError('No such user')

    # `user` is now set to the correct element
    self.root.remove(user)
    print user
    tree = ET.ElementTree(self.root)
    tree.write("msl.xml") 
Heisler answered 2/12, 2012 at 9:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.