Python处理XML

作者: Solomon_Xie | 来源:发表于2019-01-20 00:50 被阅读0次

参考:The Hitchhiker's Guide to Python: XML parsing

<mydocument has="an attribute">
  <and>
    <many>elements</many>
    <many>more elements</many>
  </and>
  <plus a="complex">
    element as well
  </plus>
</mydocument>

xmltodict

安装:pip install xmltodict --user

import xmltodict

with open('path/to/file.xml') as fd:
    doc = xmltodict.parse(fd.read())

doc['mydocument']['@has'] # == u'an attribute'
doc['mydocument']['and']['many'] # == [u'elements', u'more elements']
doc['mydocument']['plus']['@a'] # == u'complex'
doc['mydocument']['plus']['#text'] # == u'element as well'

相关文章

网友评论

    本文标题:Python处理XML

    本文链接:https://www.haomeiwen.com/subject/fdzbrqtx.html