简体   繁体   English

写入数据时出现lxml.objectify错误

[英]lxml.objectify error when writing data

here is my code: 这是我的代码:

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')

Here is parsed xml file: 这是解析的xml文件:

<?xml version="1.0" ?>
<zAppointments reminder="15">
    <appointment>
        <begin>1181251600</begin>
        <uid>0400000008200E000</uid>
        <alarmTime>1181572063</alarmTime>
        <state></state>
        <location></location>
        <duration>1800</duration>
        <subject>Bring pizza home</subject>
    </appointment>
    <appointment>
        <begin>1234567890</begin>
        <duration>1800</duration>
        <subject>Check MS office webstie for updates</subject>
        <state>dismissed</state>
        <location></location>
        <uid>502fq14-12551ss-255sf2</uid>
     </appointment>
</zAppointments>

And here is output with error: 这是有错误的输出:

/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

What can I do to turn that f object to a string? 我该怎么做才能将f对象转换为字符串? Is it possible even? 有可能吗? I got that error few times earlier and still don't know how to fix it (doing Python 101 exercises). 我之前几次收到该错误,但仍然不知道如何解决该错误(进行Python 101练习)。

obj_xml is bytes type, so can't use it with write() without decoding it first. obj_xml是字节类型,因此如果不先对其进行解码,就不能将其与write()一起使用。 Need to change 需要改变

f.write(obj_xml)

to: 至:

f.write(obj_xml.decode('utf-8'))

And it works great! 而且效果很好!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM