ElementTree SyntaxError:预期的路径分隔符([) [英] ElementTree SyntaxError: expected path separator ([)

查看:91
本文介绍了ElementTree SyntaxError:预期的路径分隔符([)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去几天,我进行了广泛的搜索,但似乎找不到我想要的东西.我已经使用Python 2.7.3和ElementTree编写了一个脚本,以解析XML文件并编辑XML文件中深处的属性.该脚本工作正常.上周晚些时候,我与该客户举行了一次会议,该客户通知我目标平台将是CentOS.我以为没问题.为了在预期的平台上进行测试,我创建了一个CentOS VMWare客户端,令我惊讶的是,我的脚本破坏了工作原理,并向我显示了错误消息" SyntaxError:预期的路径分隔符([)").通过研究此错误消息的性质,我了解到CentOS 6.4支持Python 2.6.6,其中包含一个较旧版本的ElementTree,该版本不支持搜索属性[@attribute]语法.
该客户不会在平台上升级Python,也不会安装其他库,因此lxml不是我的选择.我的问题是,在没有ElementTree对[@attribute]工具的支持的情况下,我还能以某种方式访问​​埋入的属性并对其进行编辑吗?

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?

这是我正在处理的那种XML的示例:

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>

`

下面是成功的Python 2.7.3代码的片段,该代码在Python 2.6.6下无法使用:

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') 
          ...
      ...

有没有一种方法可以修改此"if"语句块的第一行,从而使我可以编辑Fusion元素的"a​​rguments"属性?还是我被降级为实现libxml2,这肯定是一个痛苦吗?...

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?...

谢谢.

推荐答案

这可能是避免问题的方法,但是您可以尝试从Python 2.7复制并粘贴ElementTree的版本,并将其重命名以避免与冲突标准库,然后导入和使用它.

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.

但是,由于不是要把ElementTree用作独立文件,因此您需要导航到C:\Python27\Lib\xml并复制整个etree文件夹,并通过在脚本内执行import etree.ElementTree来导入ElementTree.

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.

为避免意外地从Python 2.6导入或使用ElementTree版本,您可能应该重命名etree文件夹,其内容,删除.pyc文件,并修复文件内部的导入以引用Python 2.7版本

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.

这篇关于ElementTree SyntaxError:预期的路径分隔符([)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆