首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >写入数据时出现lxml.objectify错误

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

Stack Overflow用户
提问于 2016-11-30 08:32:33
回答 1查看 272关注 0票数 0

下面是我的代码:

代码语言:javascript
复制
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文件:

代码语言:javascript
复制
<?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>

下面是带有错误的输出:

代码语言:javascript
复制
/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对象变成字符串呢?有没有可能呢?我之前遇到过几次这个错误,但仍然不知道如何修复它(做Python 101练习)。

EN

回答 1

Stack Overflow用户

发布于 2016-12-02 02:03:40

obj_xml是字节类型,所以如果不先解码它,就不能和write()一起使用。需要改变

代码语言:javascript
复制
f.write(obj_xml)

至:

代码语言:javascript
复制
f.write(obj_xml.decode('utf-8'))

而且它工作得很好!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40878294

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档