Python从门到精通(五):文件处理-04-xml文件处理(python写入xml文件的方法)

网友投稿 332 2022-08-27


Python从门到精通(五):文件处理-04-xml文件处理(python写入xml文件的方法)

python处理专用的领域一般都会有专门的库(类似java的三方开源包),在本节中我们使用的是xml库,也可以选用lxml库。

一、简单实现

14791 Clark & Balmoral 22 North Bound

North Bound
22
        5 MIN        Howard        1378        22    
        15 MIN        Howard        1867        22    

from xml.etree.ElementTree import parsedoc = parse('test.xml')# Extract and output tags of interestfor item in doc.iterfind('pre'): pt = item.findtext('pt') fd = item.findtext('fd') v = item.findtext('v') print(f'the value of pt: {pt}') print(f'the value of fd: {fd}') print(f'the value of v: {v}')print(f'doc content: {doc}')e = doc.find('pre')print(f'e is: {e}')print(f'e tag is: {e.tag}')print(f'e text value: {e.text}')print(f"e get attribute v is: {e.get('v')}")

the value of pt: 5 MINthe value of fd: Howardthe value of v: 1378the value of pt: 15 MINthe value of fd: Howardthe value of v: 1867doc content: e is: e tag is: pree text value: e get attribute v is: None

二、XML解析

David Beazley

author is: David Beazleycontent is: content/html is: Nonefind content: find text: Nonefind more: Hello Worldns find: ns text find: Hello Worldevt is: end, elem is: evt is: start-ns, elem is: ('', 'is: end, elem is: evt is: end, elem is: evt is: end, elem is: evt is: end, elem is: evt is: end, elem is: evt is: end-ns, elem is: Noneevt is: end, elem is: evt is: end, elem is: elem:

三、XML修改

14791 Clark & Balmoral 22 North Bound

North Bound
22
        5 MIN        Howard        1378        22    
        15 MIN        Howard        1867        22    

from xml.etree.ElementTree import parse, Elementdoc = parse('test.xml')root = doc.getroot()print(f'root is: {root}')root.remove(root.find('sri'))root.remove(root.find('cr'))print(f"root children index: {root.getchildren().index(root.find('nm'))}")e = Element('spam')e.text = 'This is a test'root.insert(2, e)print(f"doc write: {doc.write('newpred.xml', xml_declaration=True)}")

四、DICT与XML

from xml.etree.ElementTree import Elementdef dict_to_xml(tag, d): element = Element(tag) for key, val in d.items(): child = Element(key) child.text = str(val) element.append(child) return elementcourse_dict = {'course_name': 'python', 'total_class': 30, 'score':0.3}elem = dict_to_xml('course', course_dict)print(f'elem is: {elem}')from xml.etree.ElementTree import tostringprint(f'elem to sting is: {tostring(elem)}')elem.set('_id','1234')print(f'elem to sting is: {tostring(elem)}')#只能创建字符串类型的值def dict_to_xml_str(tag, d): part_list = [f'<{tag}>'] for key, val in d.items(): part_list.append(f'<{key}>{val}') part_list.append(f'') return ''.join(part_list)d = {'courese_name': ''}print(f"dict to xml str: {dict_to_xml_str('item',d)}")elem = dict_to_xml('item',d)print(f'elem to sting is: {tostring(elem)}')#替换xml的5种特有字符from xml.sax.saxutils import escape, unescapeprint(f"escape: {escape('')}")print(f"unescape: {unescape('_')}")

elem is: elem to sting is: b'python300.3'elem to sting is: b'python300.3'dict to xml str: elem to sting is: b'<python>'escape: <python>unescape: _


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java mysql特殊形式的查询语句详解
下一篇:Python从门到精通(五):文件处理-03-Json文件处理(python输出json文件)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~