发布时间:2019-08-01 17:27:37编辑:auto阅读(1485)
Python XML解析
----http://www.w3cschool.cn/python/python-xml.html
Python中使用ElementTree对XML文件进行解析
----http://www.jianshu.com/p/bcef2ff6ffaa
xml文件样例:
<?xml version = '1.0' encoding = 'UTF-8'?> <ADI> <Metadata> <AMS Asset_Name="夜线" Provider="Gehua" Product="MOD" Version_Major="1" Version_Minor="0" Creation_Date="2017-06-16" Provider_ID="Gehua.com" Asset_ID="GEHU8261706160621000" Asset_Class="package"/> </Metadata> <Asset> <Metadata> <AMS Asset_Name="夜线" Provider="Gehua" Product="MOD" Version_Major="1" Version_Minor="0" Creation_Date="2017-06-16" Provider_ID="Gehua.com" Asset_ID="GEHU8261706160621000" Asset_Class="title"/> <App_Data App="MOD" Name="title" Value="夜线"/> <App_Data App="MOD" Name="event_start_time" Value="2017-06-16T06:21:00"/> <App_Data App="MOD" Name="event_end_time" Value="2017-06-16T07:01:00"/> <App_Data App="MOD" Name="channel_id" Value="826"/> <App_Data App="MOD" Name="channel_name" Value="CCTV社会与法"/> <App_Data App="MOD" Name="event_status" Value="4"/> <App_Data App="MOD" Name="event_expire_time" Value="2017-06-30T07:01:00"/> <App_Data App="MOD" Name="titleabbreviation" Value="YX"/> <App_Data App="MOD" Name="titlecharacterscount" Value="2"/> <App_Data App="MOD" Name="country_of_origin" Value="中国"/> <App_Data App="MOD" Name="genre" Value="综合"/> <App_Data App="MOD" Name="schedule_id" Value="8261706160621000"/> </Metadata> </Asset> </ADI>
遍历根节点可以获得子节点,然后就可以根据需求拿到需要的字段
#!/usr/bin/python # -*- coding: utf-8 -*- import xml.etree.ElementTree as ET tree = ET.parse('D:\test\20172016062039433.xml') root = tree.getroot() print('root-tag:',root.tag,',root-attrib:',root.attrib,',root-text:',root.text) for child in root: print('child-tag:',child.tag,',child.attrib:',child.attrib,',child.text:',child.text) for sub in child: print('sub-tag:',sub.tag,',sub.attrib:',sub.attrib,',sub.text:',sub.text) for sub1 in sub: print('sub1-tag:',sub1.tag,',sub1.attrib:',sub1.attrib,',sub1.text:',sub1.text) animNode = root.find('Metadata') for animNode1 in animNode: print(animNode1.tag,animNode1.attrib,animNode1.text)
代码执行结果如下:
('root-tag:', 'ADI', ',root-attrib:', {}, ',root-text:', '\n ') ('child-tag:', 'Metadata', ',child.attrib:', {}, ',child.text:', '\n ') ('sub-tag:', 'AMS', ',sub.attrib:', {'Asset_ID': 'GEHU8261706160621000', 'Product': 'MOD', 'Asset_Class': 'package', 'Version_Major': '1', 'Provider_ID': 'Gehua.com', 'Creation_Date': '2017-06-16', 'Asset_Name': u'\u591c\u7ebf', 'Provider': 'Gehua', 'Version_Minor': '0'}, ',sub.text:', None) ('child-tag:', 'Asset', ',child.attrib:', {}, ',child.text:', '\n ') ('sub-tag:', 'Metadata', ',sub.attrib:', {}, ',sub.text:', '\n ') ('sub1-tag:', 'AMS', ',sub1.attrib:', {'Asset_ID': 'GEHU8261706160621000', 'Product': 'MOD', 'Asset_Class': 'title', 'Version_Major': '1', 'Provider_ID': 'Gehua.com', 'Creation_Date': '2017-06-16', 'Asset_Name': u'\u591c\u7ebf', 'Provider': 'Gehua', 'Version_Minor': '0'}, ',sub1.text:', None) ('sub1-tag:', 'App_Data', ',sub1.attrib:', {'App': 'MOD', 'Name': 'title', 'Value': u'\u591c\u7ebf'}, ',sub1.text:', None) ('sub1-tag:', 'App_Data', ',sub1.attrib:', {'App': 'MOD', 'Name': 'event_start_time', 'Value': '2017-06-16T06:21:00'}, ',sub1.text:', None) ('sub1-tag:', 'App_Data', ',sub1.attrib:', {'App': 'MOD', 'Name': 'event_end_time', 'Value': '2017-06-16T07:01:00'}, ',sub1.text:', None) ('sub1-tag:', 'App_Data', ',sub1.attrib:', {'App': 'MOD', 'Name': 'channel_id', 'Value': '826'}, ',sub1.text:', None) ('sub1-tag:', 'App_Data', ',sub1.attrib:', {'App': 'MOD', 'Name': 'channel_name', 'Value': u'CCTV\u793e\u4f1a\u4e0e\u6cd5'}, ',sub1.text:', None) ('sub1-tag:', 'App_Data', ',sub1.attrib:', {'App': 'MOD', 'Name': 'event_status', 'Value': '4'}, ',sub1.text:', None) ('sub1-tag:', 'App_Data', ',sub1.attrib:', {'App': 'MOD', 'Name': 'event_expire_time', 'Value': '2017-06-30T07:01:00'}, ',sub1.text:', None) ('sub1-tag:', 'App_Data', ',sub1.attrib:', {'App': 'MOD', 'Name': 'titleabbreviation', 'Value': 'YX'}, ',sub1.text:', None) ('sub1-tag:', 'App_Data', ',sub1.attrib:', {'App': 'MOD', 'Name': 'titlecharacterscount', 'Value': '2'}, ',sub1.text:', None) ('sub1-tag:', 'App_Data', ',sub1.attrib:', {'App': 'MOD', 'Name': 'country_of_origin', 'Value': u'\u4e2d\u56fd'}, ',sub1.text:', None) ('sub1-tag:', 'App_Data', ',sub1.attrib:', {'App': 'MOD', 'Name': 'genre', 'Value': u'\u7efc\u5408'}, ',sub1.text:', None) ('sub1-tag:', 'App_Data', ',sub1.attrib:', {'App': 'MOD', 'Name': 'schedule_id', 'Value': '8261706160621000'}, ',sub1.text:', None) ('AMS', {'Asset_ID': 'GEHU8261706160621000', 'Product': 'MOD', 'Asset_Class': 'package', 'Version_Major': '1', 'Provider_ID': 'Gehua.com', 'Creation_Date': '2017-06-16', 'Asset_Name': u'\u591c\u7ebf', 'Provider': 'Gehua', 'Version_Minor': '0'}, None)
上一篇: python动态语言特性小结
下一篇: Python学习--shutil
47494
45794
36792
34323
28960
25598
24443
19611
19111
17632
5466°
6048°
5570°
5638°
6573°
5376°
5378°
5884°
5855°
7170°