lxml.objectify写入d时出错

2024-04-28 07:12:48 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的代码:

from lxml import etree, objectify

def parseXML(xmlFile):
    with open(xmlFile) as f:
        xml = f.read()

    root = objectify.fromstring(xml)

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

    #how to extract element data
    begin = root.appointment.begin
    uid = root.appointment.uid

    #loop over elements and print their tags and text
    for appt in root.getchildren():
        for e in appt.getchildren():
            print('%s => %s' % (e.tag, e.text))
        print()

    #how to change element's text
    root.appointment.begin = 'something else'
    print(root.appointment.begin)

    #how to add a new element
    root.appointment.new_element = 'new data'

    #remove the py:pytype stuff
    objectify.deannotate(root)
    etree.cleanup_namespaces(root)
    obj_xml = etree.tostring(root, pretty_print=True)
    print(obj_xml)

    #save your xml
    with open('new.xml', 'w') as f:
        f.write(obj_xml)

parseXML('example.xml')

以下是已解析的xml文件:

^{pr2}$

以下是错误输出:

/usr/bin/python3.5 "/home/michal/Desktop/nauka programowania/python 101/parsing_with_lxml.py"
begin => 1181251600
uid => 0400000008200E000
Traceback (most recent call last):
alarmTime => 1181572063
  File "/home/michal/Desktop/nauka programowania/python 101/parsing_with_lxml.py", line 87, in <module>
state => None
location => None
    parseXML('example.xml')
duration => 1800
subject => Bring pizza home

begin => 1234567890
duration => 1800
subject => Check MS office webstie for updates
state => dismissed
location => None
uid => 502fq14-12551ss-255sf2

something else
b'<zAppointments reminder="15">\n  <appointment>\n    <begin>something else</begin>\n    <uid>0400000008200E000</uid>\n    <alarmTime>1181572063</alarmTime>\n    <state/>\n    <location/>\n    <duration>1800</duration>\n    <subject>Bring pizza home</subject>\n    <new_element>new data</new_element>\n  </appointment>\n  <appointment>\n    <begin>1234567890</begin>\n    <duration>1800</duration>\n    <subject>Check MS office webstie for updates</subject>\n    <state>dismissed</state>\n    <location/>\n    <uid>502fq14-12551ss-255sf2</uid>\n  </appointment>\n</zAppointments>\n'
  File "/home/michal/Desktop/nauka programowania/python 101/parsing_with_lxml.py", line 85, in parseXML
    f.write(obj_xml)
TypeError: write() argument must be str, not bytes

Process finished with exit code 1

我该怎么做才能把f对象变成字符串呢?有没有可能?我之前几次遇到这个错误,但仍然不知道如何修复它(做python101练习)。在


Tags: inhomenewuidwithrootxmlelement
1条回答
网友
1楼 · 发布于 2024-04-28 07:12:48

obj_xml是字节类型,因此如果不先解码,就不能将其与write()一起使用。需要改变吗

f.write(obj_xml)

收件人:

^{pr2}$ 很好!在

相关问题 更多 >