当前位置: 首页 > 面试题库 >

ElementTree SyntaxError: expected path separator ([)

章岳
2023-03-14
问题内容

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 file and edit an attribute buried deep within the XML file. The
script works fine. I had a meeting late last week with the customer who
informed me the target platform will be CentOS. I thought, no problem. To test
on the anticipated platform I created a CentOS VMWare client and much to my
surprise my script crapped the bed, giving me the error message, “
SyntaxError: expected path separator ([) ” In the course of my researching
the nature of this error message I learned that CentOS 6.4 supports Python
2.6.6, which contains an older version of ElementTree that does not have
support for searching for attributes [@attribute] syntax.
This customer won’t upgrade Python on the platform, nor will they install
additional libraries, so lxml is not an option for me. My question is, can I
somehow still access the buried attribute and edit it without the ElementTree
support for the [@attribute] facilities?

Here’s an example of the kind of XML I’m dealing with:

`

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<my-gui>
    <vehicles>
        <car vendor="Ford"/>
    </vehicles>
    <options>
        <line transmission='manual'/>
    </options>
    <title>Dealership</title>
    <choice id='manual' title="Dealership">
        <pkg-deal id='manual' auth='manager'>.</pkg-deal>
    </choice>
    <choice id='manual' title="Dealership"/>
    <choice id='manual' DealerLocation='Dealer_Loc'/>
    <choices-outline color='color_choice'>
        <line choice='blue'/>
    </choices-outline>
    <choice id='cars' GroupID='convertables'>
        <pkg-deal id='model.Taurus' version="SEL" arguments='LeatherInterior' enabled='XMRadio'>Taurus</pkg-deal>
        <pkg-deal id='model.Mustang' version="GT" enabled='SIRIUSRadio'>Mustang</pkg-deal>
        <pkg-deal id='model.Focus' version="SE" enabled='XMRadio'>Focus</pkg-deal>
        <pkg-deal id='model.Fairlane'>Fairlane</pkg-deal>
        <pkg-deal id='model.Fusion' version="SE" arguments='ClothInerior'>Fusion</pkg-deal>
        <pkg-deal id='model.Fiesta' version="S Hatch" enabled="SIRIUSRadio">Fiesta</pkg-deal>
    </choice>
</my-gui>

`

Here’s a snippet of the successful Python 2.7.3 code that breaks under Python
2.6.6:

if self.root.iterfind('pkg-deal'): 
          self.deal = self.root.find('.//pkg-deal[@id="model.fusion"]')
          self.arg = str(self.deal.get('arguments'))
          if self.arg.find('with Scotchguard=') > 0:  
            QtGui.QMessageBox.information(self, 'DealerAssist', 'The selected car is already updated. Nothing to do.')            
            self.leave()        
          self.deal.set('arguments', self.arg + ' with Scotchguard') 
          ...
      ...

Is there a way I can modify the first line of this ‘if’ statement block that
will allow me to edit the ‘arguments’ attribute of the Fusion element? Or am I
relegated to implementing libxml2, which promises to be a real pain?…

Thanks.


问题答案:

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.



 类似资料:

相关阅读

相关文章

相关问答