zoukankan      html  css  js  c++  java
  • python之xml 文件的读取方法

    '''
    xml 文件的读取方法
    '''
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    import xml.etree.ElementTree as ET
    
    from  datetime import datetime
    tree = ET.parse("country.xml")
    root = tree.getroot()
    print "**************tag 根元素*********"
    print(root.tag, ":", root.attrib)  # 打印根元素的tag和属性
    #   find('nodeName'):表示在该节点下,查找其中第一个tag为nodeName的节点。
    print "*#查找root节点下第一个tag为country的节点********"
    animNode = root.find('country') #查找root节点下第一个tag为country的节点
    print(animNode.tag,animNode.attrib,animNode.text)
    print "**查找其中所有tag为nodeName的节点*****"
    #   findall('nodeName'):表示在该节点下,查找其中所有tag为nodeName的节点
    # animNode = root.findall('country') #查找root节点下所有 tag为country的节点
    # print(animNode.tag,animNode.attrib,animNode.text)
    
    print "*删除指定的节点的信息  保存文件*"
    animNode = root.find('country')
    if animNode.attrib['name'] == 'Liechtenstein':
        root.remove(animNode)
    tree.write('finish.xml')  # 保存修改后的XML文件
    
    cur_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    print "current time: ",cur_time
    # 遍历xml文档的第二层
    for child in root:
        # 第二层节点的标签名称和属性
        print(child.tag,":", child.attrib)
        # 遍历xml文档的第三层
        for children in child:
            # 第三层节点的标签名称和属性
            print(children.tag, ":", children.attrib)
  • 相关阅读:
    Linux下安装Readis
    windows下的Redis安装:
    解决@ResponseBody不能和 <mvc:annotation-driven>同时使用的问题
    dom4j操作XML
    Ajax优缺点
    来一打前端博客压压惊
    tinypng upload一键压缩上传工具,告别人肉
    手把手教你撸一个简易的 webpack
    前端路由简介以及vue-router实现原理
    JS 数据类型、赋值、深拷贝和浅拷贝
  • 原文地址:https://www.cnblogs.com/wanghuixi/p/10786841.html
Copyright © 2011-2022 走看看