<?xml version="1.0" encoding="utf-8" ?>
<!--this is a test about xml.-->
<collection shelf="New Arrivals">
<movie title="Enemy Behind">
<type>War, Thriller</type>
<format>DVD</format>
<year>2003</year>
<rating>PG</rating>
<stars>10</stars>
<description>Talk about a US-Japan war</description>
</movie>
<movie title="Transformers">
<type>Anime, Science Fiction</type>
<format>DVD</format>
<year>1989</year>
<rating>R</rating>
<stars>8</stars>
<description>A schientific fiction</description>
</movie>
<movie title="Trigun">
<type>Anime, Action</type>
<format>DVD</format>
<episodes>4</episodes>
<rating>PG</rating>
<stars>10</stars>
<description>Vash the Stampede!</description>
</movie>
<movie title="Ishtar">
<type>Comedy</type>
<format>VHS</format>
<rating>PG</rating>
<stars>2</stars>
<description>Viewable boredom</description>
</movie>
</collection>
 
#练习:计算movie文件中有多少个名字叫War, Thriller的电影
import sys
try:
    import xml.etree.cElementTree as ET
except ImportError:
    import xml.etree.ElementTree as ET
 
tree = ET.parse("e:\\movie.xml")
 
count = 0
for elem in tree.iter(tag='movie'): #遍历树中的movie节点
    print elem.tag
    if elem[0].text == 'War, Thriller':
        count += 1
print count
 
#以下代码实现了边读文件边解析的作用,节省了内存
count = 0
for event, elem in ET.iterparse("e:\\movie.xml"):  #遍历所有xml文件中的标签
    #print elem.tag
    if event == 'end':  #检测“闭合的”(end)事件,标签关闭
        if elem.tag == 'type' and elem.text == 'War, Thriller':  #标签为type,且文本内容为War, Thriller ,则count+1
            count += 1
    elem.clear() #清除元素内容,不清除则整个儿树也会在内存中,没有起到节省内存的作用。
 
print count
 
 

【Python】xml遍历练习的更多相关文章

  1. Python xml 模块

    Python xml 模块 TOC 什么是xml? xml和json的区别 xml现今的应用 xml的解析方式 xml.etree.ElementTree SAX(xml.parsers.expat) ...

  2. Python XML解析之ElementTree

    参考网址: http://www.runoob.com/python/python-xml.html https://docs.python.org/2/library/xml.etree.eleme ...

  3. python 实时遍历日志文件

    首先尝试使用 python open 遍历一个大日志文件, 使用 readlines() 还是 readline() ? 总体上 readlines() 不慢于python 一次次调用 readlin ...

  4. Python XML解析(转载)

    Python XML解析 什么是XML? XML 指可扩展标记语言(eXtensible Markup Language). 你可以通过本站学习XML教程 XML 被设计用来传输和存储数据. XML是 ...

  5. python大法好——Python XML解析

    Python XML解析 什么是XML? XML 被设计用来传输和存储数据. XML是一套定义语义标记的规则,这些标记将文档分成许多部件并对这些部件加以标识. 它也是元标记语言,即定义了用于定义其他与 ...

  6. Python文件遍历二种方法

    分享下有关Python文件遍历的两种方法,使用的OS模块的os.walk和os.listdir实现. 关于Python的文件遍历,大概有两种方法,一种是较为便利的os.walk(),还有一种是利用os ...

  7. Python简单遍历字典及删除元素的方法

    Python简单遍历字典及删除元素的方法 这篇文章主要介绍了Python简单遍历字典及删除元素的方法,结合实例形式分析了Python遍历字典删除元素的操作方法与相关注意事项,需要的朋友可以参考下 具体 ...

  8. python+selenium遍历某一个标签中的内容

    一.python+selenium遍历某一个标签中的内容 举个例子:我要获取列表标签<li></li>的内容 根据python+selenium定位到列表整体,使用for循环获 ...

  9. python文件目录遍历保存成xml文件代码

    Linux服务器有CentOS.Fedora等,都预先安装了Python,版本从2.4到2.5不等,而Windows类型的服务器也多数安装了Python,因此只要在本机写好一个脚本,上传到对应机器,在 ...

随机推荐

  1. leetcode-algorithms-35 Search Insert Position

    leetcode-algorithms-35 Search Insert Position Given a sorted array and a target value, return the in ...

  2. 微信公众号使用LocalStorage解决返回缓存问题

    在开发微信公众号上应用程序时,遇到了一个普遍的问题,从A页跳转到B页后,再由B页跳转回A页,A要要保持跟跳转前一致,通过LocalStorage可以解决. LocalStorage,很好的解决了返回的 ...

  3. SAP 打开SAP物料帐期和财务账期

    引http://blog.sina.com.cn/s/blog_494f9a6b0102e8zw.html 物料账期:Tcode MMPV和Tcode MMRV 财务账期:Tcode OKP1  和O ...

  4. TP5+jquery即点既改

    //表单 {volist name="date" id="v"}<tr id="{$v.id}"> <td>< ...

  5. python中bottle模块的使用

    1.简介 2.示例 2.1一个简单的bottle接口 # -*- coding: utf-8 -*- from bottle import route, request, run import jso ...

  6. python中RabbitMQ的使用(工作队列)

    消息可以理解为任务,消息发送者可以看成任务派送者(sender),消息接收者可以看成工作者(worker). 当工作者接收到一个任务,还没完任务时分配者又发一个任务,此时需要多个工作者来共同处理这些任 ...

  7. Shelld5的使用

    Shelld的连接7步   ·   · huhu_k: 想和你相遇.

  8. ORA-01034: ORACLE not available ORA-27101

    出现ORA-01034和ORA-27101的原因是多方面的:主要是oracle当前的服务不可用,shared memory realm does not exist,是因为oracle没有启动或没有正 ...

  9. 解决 TCP_socket 粘包问题

    所谓粘包问题主要还是C/S两端数据传输时 因为接收方不知道消息之间的界限,不知道一次性提取多少字节的数据所造成的 根本原因:粘包是由TCP协议本身造成的,TCP为提高传输效率,发送方往往要收集到足够多 ...

  10. nginx配置location总结及rewrite规则写法(2)

    2. Rewrite规则 rewrite功能就是,使用nginx提供的全局变量或自己设置的变量,结合正则表达式和标志位实现url重写以及重定向.rewrite只能放在server{},location ...