这是我的代码:
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文件:
<?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>
这是输出错误:
/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练习)。
答案 0 :(得分:0)
obj_xml是字节类型,所以不能将它与write()一起使用而不先解码它。需要改变
f.write(obj_xml)
为:
f.write(obj_xml.decode('utf-8'))
它很棒!