zoukankan      html  css  js  c++  java
  • python优秀库 - 使用xmltodict解析xml文档

    上次讲到如何使用BeautifulSoup解析XML文档,今天发现另外一个python库xmltodict(https://github.com/martinblech/xmltodict)也很简单。

    小例子一枚,因为很简单,不需要太多说明

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    
    __author__ = 'liulixiang'
    import xmltodict
    
    doc = xmltodict.parse('''<?xml version="1.0"?>
    <VehicleInfo has="测试">
      <FileHeader>
        <ScaleInfo>
          <SN>H00120030101081526</SN>
          <UserName>盛隆钢铁</UserName>
          <SUMWeight>0</SUMWeight>
        </ScaleInfo>
      </FileHeader>
      <FileBody>
        <Node>
          <ID>1</ID>
          <_DateTime>2003-1-1 8:14:25</_DateTime>
          <VehicleType />
          <VehicleCardID />
          <Speed>17.5</Speed>
          <Weight>3.12</Weight>
          <PIC1>_1.bmp</PIC1>
        </Node>
        <Node>
          <ID>2</ID>
          <_DateTime>2003-1-1 8:14:26</_DateTime>
          <VehicleType />
          <VehicleCardID />
          <Speed>15.8</Speed>
          <Weight>4.77</Weight>
          <PIC1>_1.bmp</PIC1>
        </Node>
      </FileBody>
    </VehicleInfo>
    ''', encoding='utf-8')
    
    print(doc['VehicleInfo']["@has"])
    for node in doc['VehicleInfo']['FileBody']['Node']:
        print(node['ID'], node['Weight'], node['_DateTime'])
  • 相关阅读:
    创建用户自定义函数 SQL
    sql with as 用法
    将string转为同名类名,方法名。(c#反射)
    linq 实现对象映射
    sql 游标
    C#编程总结(六)异步编程
    线程加锁解锁,用于循环条件不确定大小
    滚动条随代码滚动
    C# 代码小技巧
    reload方法
  • 原文地址:https://www.cnblogs.com/liulixiang/p/3540193.html
Copyright © 2011-2022 走看看