【问题标题】:builtins.TypeError: reading file objects must return plain strings : Error in Xpath - pythonbuiltins.TypeError:读取文件对象必须返回纯字符串:Xpath 中的错误 - python
【发布时间】:2012-08-27 19:16:35
【问题描述】:

这是我的代码:

import os
os.chdir('d:/py/xml/')


from lxml import etree
from io import StringIO

#----------------------------------------------------------------------
def parseXML(xmlFile):
    """
    Parse the xml
    """
    f = open(xmlFile)
    xml = f.read()
    f.close()

    tree = etree.parse(StringIO(xml))
    context = etree.iterparse(StringIO(xml))
    for action, elem in context:
        if not elem.text:
            text = 'None'
        else:
            text = elem.text
        print (elem.tag + ' => ' + text)

if __name__ == "__main__":
    parseXML("example.xml")

我正在尝试提取下面的这个 xml 文件:

    <?xml version="1.0" ?>
<zAppointments reminder="15">
    <appointment>
        <begin>1181251680</begin>
        <uid>040000008200E000</uid>
        <alarmTime>1181572063</alarmTime>
        <state></state>
        <location></location>
        <duration>1800</duration>
        <subject>Bring pizza home</subject>
    </appointment>
    <appointment>
        <begin>1234360800</begin>
        <duration>1800</duration>
        <subject>Check MS Office website for updates</subject>
        <location></location>
        <uid>604f4792-eb89-478b-a14f-dd34d3cc6c21-1234360800</uid>
        <state>dismissed</state>
  </appointment>
</zAppointments>

遇到这个错误,不知道我做错了什么,请帮忙。

builtins.TypeError:读取文件对象必须返回纯字符串

谢谢,

【问题讨论】:

  • 你能发布完整的堆栈跟踪吗?此外,是否有创建 tree 变量的原因?如果不需要,您可以尝试将文件 f 传递给 iterparse 函数,而不是将其全部读取并从其内容中创建 StringIO 类文件对象。
  • builtins.TypeError: 读取文件对象必须返回纯字符串文件 "d:\py\xml\example.py",第 27 行,在 parseXML("example.xml") 文件中" d:\py\xml\example.py”,第 19 行,在 parseXML 中用于操作,elem 在上下文中:文件“C:\Python32\Lib\site-packages\lxml\etree.pyd”,第 491 行,在 lxml 中。 etree.iterparse.__next__ (src/lxml\lxml.etree.c:98616) 文件“C:\Python32\Lib\site-packages\lxml\etree.pyd”,第 512 行,在 lxml.etree.iterparse._read_more_events ( src/lxml\lxml.etree.c:98819)
  • 是否可以跳过StringIO并将真实文件f传递给lxml.etree.parseiter
  • HI Blckknght ,你的意思是这个 context = etree.iterparse(f)
  • 是的。您必须删除(或注释掉)上面读取和关闭 f 的早期位。

标签: python xml parsing lxml


【解决方案1】:
import os
os.chdir('d:/py/xml/')


from lxml import etree
#from io import StringIO

#----------------------------------------------------------------------
def parseXML(xmlFile):
    """
    Parse the xml
    """
    f = open(xmlFile)
    #xml = f.read()
    #f.close()

    #tree = etree.parse(StringIO(xml))
    context = etree.iterparse(f)
    for action, elem in context:
        if not elem.text:
            text = 'None'
        else:
            text = elem.text
        print (elem.tag + ' => ' + text)

if __name__ == "__main__":
    parseXML("example.xml")

所以这是最终代码以防万一有人需要它,谢谢Blckknght

【讨论】:

    猜你喜欢
    • 2012-05-01
    • 2017-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-04
    • 1970-01-01
    • 2019-03-23
    • 1970-01-01
    相关资源
    最近更新 更多