Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
478 views
in Technique[技术] by (71.8m points)

python - ElementTree SyntaxError: expected path separator ([)

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:Python27Libxml 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...