zoukankan      html  css  js  c++  java
  • ElementTree类

     elementtree主要是一个包含根节点的树的文档包装器

    它提供了序列化和一般文档处理的两种方法

    from lxml import etree
    
    str = '''<?xml version="1.0"?>
         <!DOCTYPE root SYSTEM "test" [ <!ENTITY tasty "parsnips"> ]>
         <root>
           <a>&tasty;</a>
        </root>
        '''
    root = etree.XML(str)
    
    tree = etree.ElementTree(root)
    print(tree.docinfo.xml_version) #输出:1.0
    print(tree.docinfo.doctype) #输出:<!DOCTYPE root SYSTEM "test">
    
    tree.docinfo.public_id = '-//W3C//DTD XHTML 1.0 Transitional//EN'
    tree.docinfo.system_url = 'file://local.dtd'
    
    print(tree.docinfo.doctype)
    '''输出:
    <!DOCTYPE root PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "file://local.dtd">
    '''
    
    print(etree.tostring(tree))
    '''输出:
    <!DOCTYPE root PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "file://local.dtd" [
    <!ENTITY tasty "parsnips">
    ]>
    <root>
      <a>parsnips</a>
    </root>
    '''
    
    print(etree.tostring(tree.getroot()))
    '''输出:
    <root>
      <a>parsnips</a>
    </root>
    '''

    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

  • 相关阅读:
    梯度消失、爆炸原因及其解决方法(转)
    Learning to Rank for IR的评价指标—MAP,NDCG,MRR
    tensorflow中使用指定的GPU及GPU显存 CUDA_VISIBLE_DEVICES
    深度学习 weight initialization
    python 第三方包安装
    列表操作 -深拷贝与浅拷贝
    python排序 sorted()与list.sort() (转)
    Python 第三方库 cp27、cp35 等文件名的含义(转)
    Learning to Rank(转)
    Spring MVC异常处理
  • 原文地址:https://www.cnblogs.com/shiliye/p/11765446.html
Copyright © 2011-2022 走看看