Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python lxml.objectify无法在没有引号的情况下解析attrib-need recover=Ttrue_Python_Xml_Lxml_Lxml.objectify - Fatal编程技术网

Python lxml.objectify无法在没有引号的情况下解析attrib-need recover=Ttrue

Python lxml.objectify无法在没有引号的情况下解析attrib-need recover=Ttrue,python,xml,lxml,lxml.objectify,Python,Xml,Lxml,Lxml.objectify,有人对如何使用lxml.objectify和recover=True有什么建议吗 我有一个xml,其中属性没有被引用-->name=value而不是name='value' 下面是一些示例代码。。。我无法控制XML格式,因此无法返回并更改它。etree解析确实有效 错误是 File "<string>", line unknown XMLSyntaxError: AttValue: " or ' expected, line 4, column 21 如果我没有得到答案,我必须重新

有人对如何使用
lxml.objectify
recover=True
有什么建议吗

我有一个xml,其中属性没有被引用-->name=value而不是name='value'

下面是一些示例代码。。。我无法控制XML格式,因此无法返回并更改它。
etree
解析确实有效

错误是

File "<string>", line unknown
XMLSyntaxError: AttValue: " or ' expected, line 4, column 21
如果我没有得到答案,我必须重新回答吗

import io
#p = objectify.XMLParser(recover=True)

root = objectify.fromstring(xmlSample)

# returns attributes in element node as dict
attrib = root.getattrib()

# how to extract element data
tbl = root.mytable

print("root.mytable type=%s" % type(tbl))
lxml.etree
-有效

from lxml import etree, objectify

import io
xmlIO = io.StringIO(xmlSample)

p = etree.XMLParser(recover=True)

tree = etree.parse(xmlIO, parser=p)
root = tree.getroot()
print(root.tag)
输出:

myxml

更新:

事实证明,您可以将
recover=True
选项传递到
objectify.makeparser()
以创建一个解析器,该解析器将尝试恢复格式错误的XML文档。然后可以将创建的解析器传递给
objectify.fromstring()
,如下所示:

from lxml import etree, objectify

xmlSample="""<dict>
<maptable>
  <hdterm displevel=1 autlookup entrytype=1>Source term</hdterm>
</maptable>
</dict>"""

parser = objectify.makeparser(recover=True)
root = objectify.fromstring(xmlSample, parser)

print(type(root.maptable.hdterm))
# output :
# <type 'lxml.objectify.StringElement'>

谢谢想想这个。实际的XML是非常大的文件,这种方法需要对其进行三次解析:XML->etree,etree->good_XML,good_XML->objects。另外,将数据追溯到原始XML可能会有问题。我想没有objectify.XMLParser(recover=True)?还是替换objectify解析器的方法?@frankr6591我想我找到了将
recover=True
选项传递到
objecdtify
的方法!请参阅更新部分above@frankr6591既然它解决了这个问题,就请你帮个忙。谢谢,我接受了你的回答,因为这是最好的答案。然而,它并没有解决根本问题。我最后不得不使用etree.iterparse,因为需要将recover=True和html=True结合起来,才能原谅我正在解析的xml。
from lxml import etree, objectify

xmlSample="""<dict>
<maptable>
  <hdterm displevel=1 autlookup entrytype=1>Source term</hdterm>
</maptable>
</dict>"""

parser = objectify.makeparser(recover=True)
root = objectify.fromstring(xmlSample, parser)

print(type(root.maptable.hdterm))
# output :
# <type 'lxml.objectify.StringElement'>
from lxml import etree, objectify

xmlSample="""your_xml_here"""

p = etree.XMLParser(recover=True)
well_formed_xml = etree.fromstring(xmlSample, p)
root = objectify.fromstring(etree.tostring(well_formed_xml))