【问题标题】:cannot parse from 'QString'无法从“QString”解析
【发布时间】:2016-08-19 23:23:36
【问题描述】:

我在 python 2.7 中有这段代码

from lxml import etree
def file_dialog(self):
    global root1
    fd = QtGui.QFileDialog(self)
    self.f1 = fd.getOpenFileName()
    tree1 = etree.parse(self.f1)

当我执行这个我得到这个错误 tree1 = etree.parse(self.f1) 文件“lxml.etree.pyx”,第 3310 行,在 lxml.etree.parse (src\lxml\lxml.etree.c:72517) 文件“parser.pxi”,第 1814 行,在 lxml.etree._parseDocument (src\lxml\lxml.etree.c:106226) TypeError: 无法从 'QString' 解析

这只发生在 python 2.7 上,相同的代码适用于 python 3.4

【问题讨论】:

    标签: python-2.7 python-3.x lxml


    【解决方案1】:

    我找到了这样的解决方案:

    from lxml import etree
    from StringIO import StringIO
    
    self.f1 = fd.getOpenFileName()
        f=open(self.f1)
        xml=f.read()
        f.close()
    
        tree1 = etree.parse(StringIO(xml))
    

    【讨论】:

    • 你可以使用fromstring
    猜你喜欢
    • 1970-01-01
    • 2013-08-29
    • 1970-01-01
    • 2019-03-26
    • 2020-11-02
    • 2015-03-31
    • 2018-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多