本文介绍了Builtins.TypeError:读取文件对象必须返回纯字符串:Xpath中的错误-python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码:
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文件:
I am trying to extract this xml file below :
<?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>
遇到此错误,不确定我做错了什么,请帮忙.
Got this error, not sure what I have done wrong , please help.
builtins.TypeError:读取文件对象必须返回纯字符串
builtins.TypeError: reading file objects must return plain strings
谢谢
推荐答案
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")
因此,这是最终代码,以防万一有人需要,谢谢您
So here's the final code just in case someone needs it, Thank you Blckknght
这篇关于Builtins.TypeError:读取文件对象必须返回纯字符串:Xpath中的错误-python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!