ElementTree中的getchildren and getiterator

 36 # C:\Users\jeguan\Desktop\Test_1.txt
 37 #
 38 # <?xml version="1.0"?>
 39 # <data>
 40 #       <country name="Liechtenstein">
 41 #           <rank>1</rank>
 42 #           <year>2008</year>
 43 #           <gdppc>141100</gdppc>
 44 #           <neighbor name="Austria" direction="E"/>
 45 #           <neighbor name="Switzerland" direction="W"/>
 46 #       </country>
 47 #       <country name="Singapore">
 48 #           <rank>4</rank>
 49 #           <year>2011</year>
 50 #           <gdppc>59900</gdppc>
 51 #           <neighbor name="Malaysia" direction="N"/>
 52 #       </country>
 53 #       <country name="Panama">
 54 #           <rank>68</rank>
 55 #           <year>2011</year>
 56 #           <gdppc>13600</gdppc>
 57 #           <neighbor name="Costa Rica" direction="W"/>
 58 #           <neighbor name="Colombia" direction="E"/>
 59 #       </country>
 60 # </data>
 61 #######################################################################
 62 def func2():
 63     from xml.etree import ElementTree
 64     xlsfile = r'C:\Users\jeguan\Desktop\Test_1.txt'
 65
 66     root = ElementTree.parse(xlsfile).getroot()
 67    <span style="color:#FF0000;"><strong><span style="font-size:18px;"> ListNode = root.getiterator('rank')</span></strong></span>
 68     print(ListNode)
 69     # the output for print(ListNode) [<Element 'rank' at 0x6ffffc51450>, <Element 'rank' at 0x6ffffc51810>, <Element 'rank' at 0x6ffffc51950>]
 70     <span style="font-size:18px;color:#FF0000;"><strong>children = root.getchildren()</strong></span>
 71     print(children)
 72     # the output for print(children) is [<Element 'country' at 0x6ffffc50410>, <Element 'country' at 0x6ffffc507d0>, <Element 'country' at 0x6ffffc50910>]
https://github.com/double12gzh/PythonLearning/blob/master/ElementTree.py

转载于:https://www.cnblogs.com/double12gzh/p/10166231.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值