解析XML的Python模块:xmltodict

jopen 10年前

xmltodict是一个Python模块,可以让操作XML像操作JSON一样简便。

>>> doc = xmltodict.parse("""  ... <mydocument has="an attribute">  ...   <and>  ...     <many>elements</many>  ...     <many>more elements</many>  ...   </and>  ...   <plus a="complex">  ...     element as well  ...   </plus>  ... </mydocument>  ... """)  >>>  >>> 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'

项目主页:http://www.open-open.com/lib/view/home/1389015578203