zoukankan      html  css  js  c++  java
  • 人生苦短,我学python之python xml数据解析

    <?xml version="1.0"?>
    <data>
    <country name="Liechtenstein">
    <rank>1</rank>
    <year>2008</year>
    <gdppc>141100</gdppc>
    <neighbor name="Austria" direction="E"/>
    <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
    <rank>4</rank>
    <year>2011</year>
    <gdppc>59900</gdppc>
    <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
    <rank>68</rank>
    <year>2011</year>
    <gdppc>13600</gdppc>
    <neighbor name="Costa Rica" direction="W"/>
    <neighbor name="Colombia" direction="E"/>
    </country>
    </data>

    >>> os.getcwd()
    'D:\workspace\testpython'
    >>> import xml.etree.ElementTree as ET
    >>> tree = ET.parse('test.xml')
    >>> root = tree.getroot()
    >>> print root
    <Element 'data' at 0x1d2a8b0>
    >>> print tree
    <xml.etree.ElementTree.ElementTree object at 0x01D2A9D0>
    >>> root.tag
    'data'
    >>> root.attrib
    {}
    >>> #遍历子节点
    >>> for child in root:
    print child.tag,child.attrib


    country {'name': 'Liechtenstein'}
    country {'name': 'Singapore'}
    country {'name': 'Panama'}
    >>> root[0].text
    ' '
    >>> root[0][1].text
    '2008'
    >>> root[1][3].text
    >>> root[1][2].text
    '59900'

  • 相关阅读:
    svn不提交.net项目中的bin
    java 与C# 时间格式 交互
    生成菜单序列号
    ashx 方法模板
    webservice使用方法
    如何配置IIS服务器?
    zepto.fullpage
    模拟单选框
    star score
    Hover show tips
  • 原文地址:https://www.cnblogs.com/w770762632/p/9388830.html
Copyright © 2011-2022 走看看