zoukankan      html  css  js  c++  java
  • 14.python的xml操作

    #-*-coding:UTF-8-*-
    #python xml 文件操作
    import xml.etree.ElementTree as etree       #ElementTree属于python标准库的一部分
    tree=etree.parse('feed.xml')    #parse()函数会立即解析完整个文档,返回一个代表整片文档的对象
    root=tree.getroot()             #得到根元素
    print root.tag                 #得到跟元素标签
    print len(root)                #得到根元素下一共有几个子元素
    
    for child in root:
        print child
    #获取属性
    print root.attrib              #得到元素的属性集合,为一个字典
    
    print root[0]                  #取其第一个子元素
    
    #查找结点
    print root.findall('c')         #找到则返回这个结点,没有则为一个空列表,只会搜索子元素
    
    print root[0].find('b1')        #返回找到的第一个元素
    
    #生成XML
    new_feed=etree.Element('e')
    print new_feed

  • 相关阅读:
    桶排序
    基数排序
    计数排序
    归并排序
    快速排序
    优先级队列-堆实现
    堆排序
    红黑树
    【转】二叉树
    ubuntu 16.04 mysql 相关
  • 原文地址:https://www.cnblogs.com/chenjianhong/p/4145129.html
Copyright © 2011-2022 走看看