ElementTree SyntaxError: expected path separator ([)

后端 未结 2 1442
深忆病人
深忆病人 2020-12-21 07:30

I\'ve searched extensively for the past few days and can\'t seem to find what I\'m looking for. I\'ve written a script using Python 2.7.3 and ElementTree to parse an XML fi

相关标签:
2条回答
  • 2020-12-21 07:48

    This may be side-stepping the question, but you could just try copying-and-pasting the version of ElementTree from Python 2.7, renaming it to avoid conflicting with the standard library, and importing and using that.

    However, since ElementTree isn't meant to be used as a standalone file, what you need to do is navigate to C:\Python27\Lib\xml and copy the entire etree folder and import ElementTree by doing import etree.ElementTree inside your script.

    To avoid accidentally importing or using the version of ElementTree from Python 2.6, you should probably rename the etree folder, its contents, delete the .pyc files, and fix the imports inside the file to reference the Python 2.7 version.

    0 讨论(0)
  • 2020-12-21 07:56

    This same problem was solved by another user here.

    This user filtered the attribute manually in Python 2.6. I'm posting their code example here even though the example pertains specifically to the asker's code:

    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") 
    
    0 讨论(0)
提交回复
热议问题